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

8

18

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

Chalone answered 2/6, 2010 at 13:51 Comment(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.Alegar
L
97

Easisest way:

$time = strtotime('monday this week');
Landwehr answered 3/11, 2010 at 13:27 Comment(8)
Hah! When they say "Parse about any English textual datetime description into a Unix timestamp", they really mean it, don't they? +1 for most readable solution.Calumniation
+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.Reluctance
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 weekOmmiad
I think "monday this week" actually gives next monday.... "last monday" worked for meSydel
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.Overact
this returns seconds, wrap it like this date('Y-m-d', strtotime('monday this week'));Gynaeco
for next monday: echo date('Y-m-d', strtotime('next monday'));Pompeii
I had to use 'next monday' and 'last monday'Haemachrome
S
14

Try this:

return strtotime('last monday', strtotime('next sunday'));
Stendhal answered 2/6, 2010 at 14:13 Comment(3)
And what if today is sunday? :)Houston
@Houston - then it should still act as desired (assuming Sunday is the start of the week)Stendhal
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'))Sphagnum
E
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.

Electronarcosis answered 2/6, 2010 at 14:6 Comment(4)
1 for monday, 2 tuesday, 3 wednesday and so on :) have a try..Electronarcosis
Well this doesn't give previous Monday if you try this code on Sunday!Telegonus
@Royertan is right! I'm using dhtmlxSchedule when today is Sunday the previous Monday's data disappears, the whole week actually! Any suggestions?Supranatural
@Waiyl Karim This Maybe Help! $sunday = date('Y-m-d', strtotime('last Sunday')); $convertSunday = strtotime($sunday); $nextSunday = strtotime("+7 day", $convertSunday);Telegonus
H
8
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
}
Houston answered 2/6, 2010 at 13:59 Comment(4)
This gives a date of one week ago if today is a Monday.Stendhal
@nickf: see updated answer. I remembered that after I posted.. :)Houston
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.Huskey
@Kevin: I'm not sure I'm following you. What's precisely the difference between previous and ... last, I believe?Houston
C
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

Coniology answered 2/6, 2010 at 14:53 Comment(0)
H
3

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.

Houston answered 2/6, 2010 at 14:31 Comment(0)
S
2

Try this

$day_of_week = date("N") - 1; 
$monday_time = strtotime("-$day_of_week days");
Senseless answered 13/8, 2013 at 9:37 Comment(0)
L
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";
?>
Lentamente answered 2/6, 2010 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.