通过php找到一周的第一天[复制]

时间:2022-04-20 08:17:10

Possible Duplicate:
Get first day of week in PHP?

可能重复:获取PHP中的第一天?

Hi,

I want to find first and last date of current week and last week. Similarly I want to find first and last date of current month and last month.

我想找到本周和上周的第一个和最后一个日期。同样,我想查找当月和上个月的第一个和最后一个日期。

This has to be done in PHP. Please help.

这必须在PHP中完成。请帮忙。

2 个解决方案

#1


43  

strtotime is quite powerful with relative time formats:

strtotime相对时间格式非常强大:

strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');

(this only works with PHP 5.3+)

(这仅适用于PHP 5.3+)

strtotime('first day of this month');
strtotime('last day of this month');
strtotime('first day of last month');
strtotime('last day of last month');

In order to get the first and last date of a month in PHP < 5.3, you can use a combination of mktime and date (date('t') gives the number of days of the month):

为了获得PHP <5.3中一个月的第一个和最后一个日期,您可以使用mktime和date的组合(date('t')给出一个月的天数):

mktime(0,0,0,null, 1); // gives first day of current month
mktime(0,0,0,null, date('t')); // gives last day of current month

$lastMonth = strtotime('last month');
mktime(0,0,0,date('n', $lastMonth), 1); // gives first day of last month
mktime(0,0,0,date('n', $lastMonth), date('t', $lastMonth); // gives last day of last month

If you just want to get a string for presentation, then you don't need mktime:

如果你只想获得一个字符串进行演示,那么你不需要mktime:

date('Y-m-1'); // first day current month
date('Y-m-t'); // last day current month
date('Y-m-1', strtotime('last month')); // first day last month
date('Y-m-t', strtotime('last month')); // last day last month

#2


3  

Here's a function for the first and last day of the week:

这是一周中第一天和最后一天的函数:

function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y') 
{ 
    $wk_ts  = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101')); 
    $mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts); 
    return date($format, $mon_ts); 
} 

$sStartDate = week_start_date($week_number, $year); 
$sEndDate   = date('F d, Y', strtotime('+6 days', strtotime($sStartDate))); 

It can probably be adapted to do month as well, but I wanted to get my answer in! :)

它也许可以适应月份,但我想得到答案! :)

#1


43  

strtotime is quite powerful with relative time formats:

strtotime相对时间格式非常强大:

strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');

(this only works with PHP 5.3+)

(这仅适用于PHP 5.3+)

strtotime('first day of this month');
strtotime('last day of this month');
strtotime('first day of last month');
strtotime('last day of last month');

In order to get the first and last date of a month in PHP < 5.3, you can use a combination of mktime and date (date('t') gives the number of days of the month):

为了获得PHP <5.3中一个月的第一个和最后一个日期,您可以使用mktime和date的组合(date('t')给出一个月的天数):

mktime(0,0,0,null, 1); // gives first day of current month
mktime(0,0,0,null, date('t')); // gives last day of current month

$lastMonth = strtotime('last month');
mktime(0,0,0,date('n', $lastMonth), 1); // gives first day of last month
mktime(0,0,0,date('n', $lastMonth), date('t', $lastMonth); // gives last day of last month

If you just want to get a string for presentation, then you don't need mktime:

如果你只想获得一个字符串进行演示,那么你不需要mktime:

date('Y-m-1'); // first day current month
date('Y-m-t'); // last day current month
date('Y-m-1', strtotime('last month')); // first day last month
date('Y-m-t', strtotime('last month')); // last day last month

#2


3  

Here's a function for the first and last day of the week:

这是一周中第一天和最后一天的函数:

function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y') 
{ 
    $wk_ts  = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101')); 
    $mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts); 
    return date($format, $mon_ts); 
} 

$sStartDate = week_start_date($week_number, $year); 
$sEndDate   = date('F d, Y', strtotime('+6 days', strtotime($sStartDate))); 

It can probably be adapted to do month as well, but I wanted to get my answer in! :)

它也许可以适应月份,但我想得到答案! :)