CodeOverload
CodeOverload

Reputation: 48505

Generate a date range of the latest 2 weeks?

I want to generate 2 DATETIME which represent the latest 2 weeks starting from sunday to saturday 2x, It shouldn't include the current incomplete week.

Appreciate your help.

Upvotes: 2

Views: 893

Answers (3)

borayeris
borayeris

Reputation: 2650

<?php
$lastSaturday = strtotime("last Saturday");
//$firstSunday = $lastSaturday - (13 * 24 * 3600);
for($n=13;$n>=0;$n--){
    $timeArray[] = $lastSaturday - ($n * 24 * 3600);
    $dateTime[] = date('Y-m-d H:i l', $lastSaturday - ($n * 24 * 3600));
}
?>
<pre>
<?php
print_r($dateTime);
?>
</pre>

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237975

Something to work from, using the wonderful DateTime classes:

<?php

$end = new DateTime('last Sunday'); // note that the end date is excluded from a DatePeriod
$start = clone $end;
$start->sub(new DateInterval('P14D'));

foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $day) {
    echo $day->format('r'), "\n";
}

Upvotes: 4

dev-null-dweller
dev-null-dweller

Reputation: 29482

something to begin with:

$timestamp_end = strtotime("last Saturday");
$timestamp_start = $timestamp_end - 14 * 24 * 3600;

Upvotes: 3

Related Questions