Jump to content

Get previous Sunday


Cep

Recommended Posts

Hello,

This is a quick question but how would I go about returning the last Sunday's date in the week from the time the user views the page?

Would I use getdate() or time() with some sort of +/- combination?

Link to comment
Share on other sites

[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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.