最简单的方法是使用
date,它可以让您将硬编码的值与从时间戳提取的值进行混合。如果您不提供时间戳记,它将以当前日期和时间为准。
// Current timestamp is assumed, so these find first and last day of THIS month$first_day_this_month = date('m-01-Y'); // hard-pred '01' for first day$last_day_this_month = date('m-t-Y');// With timestamp, this gets last day of April 2010$last_day_april_2010 = date('m-t-Y', strtotime('April 21, 2010'));date()在给定的字符串(例如
'm-t-Y')中搜索特定的符号,并将其替换为时间戳中的值。因此,我们可以使用这些符号从时间戳中提取所需的值和格式。在以上示例中:
Y
为您提供时间戳记的4位数字年份(“ 2010”)m
为您提供时间戳中的数字月份,并带有前导零(‘04’)t
为您提供时间戳记月份中的天数(“ 30”)
您可以借此发挥创造力。例如,要获取一个月的第一秒和最后一秒:
$timestamp = strtotime('February 2012');$first_second = date('m-01-Y 00:00:00', $timestamp);$last_second = date('m-t-Y 12:59:59', $timestamp); // A leap year!


