I am trying to find the date range for "this week" using the following PHP code:
<?php
date_default_timezone_set('Europe/London');
$dateFrom = new DateTime('Monday this week');
$dateTo = new DateTime('Sunday this week');
$periodTextFrom = $dateFrom->format('Y/m/d 00:00:00');
$periodTextTo = $dateTo->format('Y/m/d 23:59:59');
echo $periodTextFrom.'<br>';
echo $periodTextTo.'<br>';
?>
If I run today (Sunday 19th July 2015) it gives me the following output:
2015/07/20 00:00:00
2015/07/26 23:59:59
that is all in the future and not "this" week at all.
Why is this and how can I correct?
This should work for you :
$today = getdate();
$weekStart = $today['mday'] - $today['wday'];
$weekEnd = $today['mday'] - $today['wday']+6;
echo "week start:".$today['year']."/".$today['mon'].'/'.$weekStart."
week end:".$today['year']."/".$today['mon'].'/'.$weekEnd;