简体   繁体   中英

Display the 1st, 2nd, 3rd, 4th, & 5th date of the current week in php

Ok, So im trying to figure out the best way in php to write (or print) the 1st, 2nd, 3rd, 4th, & 5th date of the current week (starting with Monday) in php.

For example... Using the week of the 4th through the 8th of February...

I would need a script for the 1st day of the week (starting with Monday) displayed as February 04, 2013 and then I would need the same script four more times to display Tues, Wed, Thurs, & Fri...

All-together I would end up with 5 scripts or one script that I could copy and manipulate to work the way I need it to...

Also, If you could tell me how to do the same for Saturday and Sunday that would be greatly appreciated as-well.

If there is anything that you do not understand, please let me know and I will try my hardest to clarify...

Thanks in advance!

First you need to get the "current" week's Monday. To do this, I would suggest calling date("N") and see if it's 1. If it is, then now is the Monday you want. Otherwise last monday is. Pass that to strtotime to get the timestamp corresponding to the first monday of the week. Then repeatedly add 24 hours (24*3600 seconds) to get each day.

$startofweek = date("N") == 1 ? time() : strtotime("last monday");
for($i=0; $i<5; $i++) {
    $day = $startofweek + 24*3600*$i;
    echo "Day ".($i+1).": ".date("d/M/Y",$day)."<br />";
}

You can use strtotime with special parameters. like:

$time = strtotime('monday this week');
$time = strtotime('today');
$time = strtotime('next monday');
$time = strtotime('previous monday');

Same goes for every other days of the week

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM