Jump to content

Computing day of week


AV1611

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?

Link to comment
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]

Link to comment
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
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.