9

I'm trying to use PHP to create a script that searches all the days between now and one year's time and lists all the dates for Fridays and Saturdays. I was trying to use PHP's date() and mktime() functions but can't think of a way of doing this. Is it possible?

Thanks, Ben

Ben
  • 159
  • 1
  • 3
  • 6

4 Answers4

11

Here's how to do it in a cool way, with special thanks to strtotime's relative formats.

$friday = strtotime('Next Friday', time());
$saturday = strtotime('Next Saturday', time());
$friday = strtotime('+1 Week', $friday);
$saturday = strtotime('+1 Week', $saturday);

Of course you should tweak it to do exactly what you want, but that's beside the point I was trying to make.

Also note that strtotime will give you timestamps. To find out the date use:

date('Y-m-d', $friday)

Another thing to know is that Next <dayofweek> will exclude your current day from the search, so if you also want to include the current day you can do it like this:

$friday = strtotime('Next Friday', strtotime('-1 Day', time()));

And here's a full working script that does exactly what you wanted.

<?php
// prevent multiple calls by retrieving time once //
$now = time();
$aYearLater = strtotime('+1 Year', $now);

// fill this with dates //
$allDates = Array();

// init with next friday and saturday //
$friday = strtotime('Next Friday', strtotime('-1 Day', $now));
$saturday = strtotime('Next Saturday', strtotime('-1 Day', $now));

// keep adding days untill a year has passed //
while(1){
    if($friday > $aYearLater)
        break 1;
    $allDates[] = date('Y-m-d', $friday);
    if($saturday > $aYearLater)
        break 1;
    $allDates[] = date('Y-m-d', $saturday);

    $friday = strtotime('+1 Week', $friday);
    $saturday = strtotime('+1 Week', $saturday);
}

//XXX: debug
var_dump($allDates);

?>

Good luck, Alin

Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
5

With DateTime objects:

define('FRIDAY', 5);
define('SATURDAY', 6);

$from = new DateTimeImmutable();
$to = new DateTimeImmutable('+1 year');

for ($date = $from; $date < $to; $date->modify('+1 day')) {
    switch ($date->format('w')) {
        case FRIDAY:
        case SATURDAY:
            echo $date->format('r') . PHP_EOL;
    }
}

Before PHP/5.5.0 you had to use regular DateTime class and clone it:

$from = new DateTime();
$to = new DateTime('+1 year');

for ($date = clone $from; $date < $to; $date->modify('+1 day')) {
    switch ($date->format('w')) {
        case FRIDAY:
        case SATURDAY:
            echo $date->format('r') . PHP_EOL;
    }
}
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
2
$secondsperday=86400;

$firstdayofyear=mktime(12,0,0,1,1,2010);
$lastdayofyear=mktime(12,0,0,12,31,2010);

$theday = $firstdayofyear;

for($theday=$firstdayofyear; $theday<=$lastdayofyear; $theday+=$secondsperday) {
    $dayinfo=getdate($theday);
    if($dayinfo['wday']==5 or $dayinfo['wday']==6) {
        print $dayinfo['weekday'].' '.date('Y-m-d',$theday)."<br />";
    }
}
Spudley
  • 166,037
  • 39
  • 233
  • 307
  • 1
    Calculations that use 86400 take for granted that all days have 24 hours, which is not always the case. Next Saturday has 25 hours in Spain due to daylight saving time. – Álvaro González Oct 28 '10 at 16:37
  • Also note that daylight saving rules varies greatly across the world. This is why you should use the built in language features when manipulating time. – Alin Purcaru Oct 28 '10 at 16:46
  • Daylight saving evens out over the course of a year. Wherever you are, it always comes into effect in the middle of the night, so best not to use midnight as your baseline, but other than that for this purpose you should be safe adding 86400. – Spudley Oct 29 '10 at 08:01
1
    $number_of_days_from_now = 365;
    $now = time();

    $arr_days = array();

    $i = 0;
    while($i <> $number_of_days_from_now){
        $str_stamp = "- $i day";
        $arr_days[] = date('Y-m-d',strtotime($str_stamp,$now));
        $i ++;
    }

    var_dump($arr_days);

I did something similar to the accepted answer that didn't suited to me

Julio Marins
  • 10,039
  • 8
  • 48
  • 54