开发者

Get date of Monday in current week in PHP 4 [duplicate]

开发者 https://www.devze.com 2023-01-02 04:45 出处:网络
This question already has answers here: 开发者_JS百科Get date for monday and friday for the current week (PHP)
This question already has answers here: 开发者_JS百科 Get date for monday and friday for the current week (PHP) (9 answers) Closed 9 years ago.

I need to find the date of Monday in the current week. How can I do this in PHP 4?


Easisest way:

$time = strtotime('monday this week');


Try this:

return strtotime('last monday', strtotime('next sunday'));


echo date('Y-m-d',time()+( 1 - date('w'))*24*3600);

For next week:

echo date('Y-m-d',time()+( 8 - date('w'))*24*3600);

1 for Monday, 2 Tuesday, 3 Wednesday and so on. Have a try.


echo date('Y-m-d', strtotime('previous monday'));

Just one note, though. You want to make sure that it's not monday today, otherwise you will get date of previous monday, just like it says. You could do it as follows

if (date('w') == 1)
{
    // today is monday
}
else
{
    // find last monday
}


$thisMonday = date('l, F d, Y', time() - ((date('w')-1) * 86400) );

Edit: explanation

  • date('w') is a numeric representation of the day of the week (0=sunday, 6=saturday)
  • there are 86400 seconds in a day
  • we take the current time, and subtract (one day * (day of the week - 1))

So, if it is currently wednesday (day 3), monday is two days ago:

time() - (86400 * (3 - 1)) = time() - 86400 * 2

If it is monday (day 1), we get:

time() - (86400 * (1 - 1)) = time() - 86400 * 0 = time()

If it is sunday (day 0), monday is tomorrow.

time() - (86400 * (0 - 1)) = time() - -86400 = time() + 86400


Try this.

echo date('Y-m-d', strtotime('last monday', strtotime('next monday')));

It will return current date if today is monday, and will return last monday otherwise. At least it does on my PHP 5.2.4 under en_US locale.


Try this

$day_of_week = date("N") - 1; 
$monday_time = strtotime("-$day_of_week days");


My attempt:

<?php
$weekday = date("w") - 1;
if ($weekday < 0)
{
    $weekday += 7;
}
echo "Monday this week : ", date("Y-m-d",time() - $weekday * 86400) , "\n";
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消