Jump to content

Archived

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

AV1611

Computing day of week

Recommended Posts

I have a script that needs to use a date range.  the starting date is not important, but the ending date is always the date of the last sunday before today.

Is there a way to compute the date of last sunday automatically?

Share this post


Link to post
Share on other sites
There is a php function to calculate the current day of the week mcal_day_of_week( int year, int month, int day ) (<a href="http://php.belnet.be/manual/en/function.mcal-day-of-week.php">see php manual</a>) and returns the day of the week of the given date. Possible return values range from 0 for Sunday through 6 for Saturday.

For ex.
[code]
$day_of_week_today = mcal_day_of_week(strftime('%Y'), strftime('%m'), strftime('%d'));
$previous_sundag = date('Y-m-d', mktime('', '', '', strftime('%m'), strftime('%d') - $day_of_week_today, strftime('%Y'));
[/code]

Share this post


Link to post
Share on other sites
can't you just use strtotime? try something like this and see if it helps:
[code]
<?php
echo date('Y-m-d', strtotime("last Sunday"));
?>
[/code]

Share this post


Link to post
Share on other sites
You can also get the date of the last sunday by using the date() and strtotime() functions:
[code]<?php
$dt = (isset($_GET['d'])?date('Y-m-d',strtotime('last sunday')):date('Y-m-d',strtotime('last sunday',strtotime($_GET['d'])));
echo $dt;
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

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.