Jump to content


Photo

Computing day of week


  • Please log in to reply
3 replies to this topic

#1 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 06 July 2006 - 12:10 PM

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?



#2 CheesierAngel

CheesierAngel
  • Members
  • PipPipPip
  • Advanced Member
  • 105 posts
  • LocationBelgium

Posted 06 July 2006 - 01:00 PM

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...f-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.
$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'));



#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 06 July 2006 - 01:21 PM

can't you just use strtotime? try something like this and see if it helps:
<?php
echo date('Y-m-d', strtotime("last Sunday"));
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 July 2006 - 01:24 PM

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

Ken





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users