13

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

random
  • 9,324
  • 10
  • 63
  • 77
Ben Jones
  • 139
  • 1
  • 1
  • 3
  • 1
    I really feel for you having to work in PHP4, because PHP5's date functionality is *waaaay* better. Also PHP4 has been officially End-Of-Life for a while already, so it won't get any more patches and it is probably insecure -- it shouldn't be in production use any more anyway. – Spudley Nov 03 '10 at 13:36

8 Answers8

90

Easisest way:

$time = strtotime('monday this week');
Dan Soap
  • 9,639
  • 1
  • 37
  • 48
Davidcologne
  • 909
  • 6
  • 2
  • 4
    Hah! When they [say](http://php.net/manual/en/function.strtotime.php) "Parse about any English textual datetime description into a Unix timestamp", they really mean it, don't they? +1 for most readable solution. – Matt Gibson Nov 03 '10 at 13:33
  • +1! Really easy solution. Glad I searched for this, because I was already building some elaborate construction to determine the date of monday. Typo though: `strotime` should be `strtotime`. – Alec Sep 11 '11 at 23:19
  • 1
    today is thursday 2012/05/17 (Y-m-d), when I do echo(date('Y-m-d', strtotime('monday this week'))); it goes 2012/05/21 which is the monday next week – r4ccoon May 17 '12 at 04:35
  • 2
    I think "monday this week" actually gives next monday.... "last monday" worked for me – Ezequiel Aug 16 '12 at 20:07
  • According to the PHP manual, "relative time formats supplied to the time argument of strtotime() such as this week, previous week, last week, and next week were interpreted to mean a 7 day period relative to the current date/time, rather than a week period of Monday through Sunday." This means that Monday is calculated based on the time now, not the current week. So depending on the day of the week, it changes. The easiest way is not always the best way. – taevanbat Jul 14 '13 at 04:37
  • this returns seconds, wrap it like this `date('Y-m-d', strtotime('monday this week'));` – d.raev May 25 '17 at 20:44
  • 1
    for next monday: echo date('Y-m-d', strtotime('next monday')); – user3162878 Jun 02 '17 at 18:35
  • I had to use 'next monday' and 'last monday' – Alejandro Cumpa Sep 22 '18 at 11:40
12

Try this:

return strtotime('last monday', strtotime('next sunday'));
nickf
  • 499,078
  • 194
  • 614
  • 709
  • 1
    And what if today is sunday? :) – mr.b Jun 02 '10 at 14:25
  • @mr.b - then it should still act as desired (assuming Sunday is the start of the week) – nickf Jun 02 '10 at 23:17
  • This is my favorite solution for getting Monday at 00:00:00. Another (less elegant) way to get this is `mktime(0,0,0,date('m'),date('j')-date('N')+1,date('Y'))` – neokio Jul 04 '12 at 08:14
10
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.

random
  • 9,324
  • 10
  • 63
  • 77
apis17
  • 2,645
  • 2
  • 21
  • 23
  • 1 for monday, 2 tuesday, 3 wednesday and so on :) have a try.. – apis17 Jun 02 '10 at 14:32
  • 2
    Well this doesn't give previous Monday if you try this code on Sunday! – ErcanE Mar 08 '15 at 22:48
  • @Royertan is right! I'm using `dhtmlxSchedule` when today is Sunday the previous Monday's data disappears, the whole week actually! Any suggestions? – Waiyl Karim Aug 16 '15 at 13:02
  • @Waiyl Karim This Maybe Help! $sunday = date('Y-m-d', strtotime('last Sunday')); $convertSunday = strtotime($sunday); $nextSunday = strtotime("+7 day", $convertSunday); – ErcanE Aug 19 '15 at 20:14
7
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
}
mr.b
  • 4,771
  • 11
  • 33
  • 53
  • 1
    This gives a date of one week ago if today is a Monday. – nickf Jun 02 '10 at 14:01
  • @nickf: see updated answer. I remembered that after I posted.. :) – mr.b Jun 02 '10 at 14:03
  • 1
    Careful with the verbiage you use with strtotime, 'previous monday' will get you a week ago on mondays, when you would really want the current day. IIRC, this week in php means monday thru sunday, but if you were to apply your answer to finding the date of tuesday for example then this would give you the wrong result on both monday and tuesday. – Kevin Vaughan Jun 02 '10 at 14:04
  • 1
    @Kevin: I'm not sure I'm following you. What's precisely the difference between previous and ... last, I believe? – mr.b Jun 02 '10 at 14:09
6

$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

Rob
  • 7,646
  • 3
  • 32
  • 36
2

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.

mr.b
  • 4,771
  • 11
  • 33
  • 53
1

Try this

$day_of_week = date("N") - 1; 
$monday_time = strtotime("-$day_of_week days");
Okochea
  • 81
  • 1
  • 2
0

My attempt:

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