Jump to content

Get previous Sunday


Cep

Recommended Posts

[code]<?php
//Get today
$dayInWeek = date("N");
$theDay = time();

//Subtract days until it's the 7th day, Sunday
while($dayInWeek != 7){
    $theDay = $theDay-(24*60*60);
    $dayInWeek = date("N");
}

//The last Sunday was $theDay;
print date("m-d-Y", $theDay);
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/35533-get-previous-sunday/#findComment-168210
Share on other sites

I would use the [url=http://www.php.net/strtotime]strtotime()[/url] function:
[code]<?php
echo 'Today is ' . date('l, F j, Y'). '<br>';
echo 'The previous Sunday was ' . date('l, F j, Y',strtotime('previous sunday')) . '<br>';
echo 'Next Sunday is ' . date('l, F j, Y',strtotime('this sunday'));
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/35533-get-previous-sunday/#findComment-168216
Share on other sites

[quote author=mjlogan link=topic=123847.msg512433#msg512433 date=1169657458]
[code=php:0]return date("l, jS F, Y", strtotime("last sunday", time()));[/code]
[/quote]
I feel like such a doofus. That is a much easier, faster way to do it. Kudos ;)
Link to comment
https://forums.phpfreaks.com/topic/35533-get-previous-sunday/#findComment-168218
Share on other sites

[quote author=jesirose link=topic=123847.msg512436#msg512436 date=1169657549]
[quote author=mjlogan link=topic=123847.msg512433#msg512433 date=1169657458]
[code=php:0]return date("l, jS F, Y", strtotime("last sunday", time()));[/code]
[/quote]
I feel like such a doofus. That is a much easier, faster way to do it. Kudos ;)
[/quote]

I have just written a massive system that does a lot of processing of data based around dates. Daylight saving time is fun to work with!  ;)
Link to comment
https://forums.phpfreaks.com/topic/35533-get-previous-sunday/#findComment-168223
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.