Get names of days between two dates in PHP

How do I get the name of the days between two dates in PHP?

Input:

Start date: 01-01-2013 End
 date: 05-01-2013

Output:

Tuesday
   Wednesday
   Thursday
   Friday
   Saturday

Trial code

$from_date ='01-01-2013';
$to_date ='05-01-2013';

$number_of_days = count_days(strtotime($from_date),strtotime($to_date));

for($i = 1; $i<=$number_of_days; $i++)
{
    $day = Date('l',mktime(0,0,0,date('m'),date('d')+$i,date('y')));
    echo "<br>".$day;       
}


function count_days( $a, $b )
{       
    $gd_a = getdate( $a );
    $gd_b = getdate( $b );

    $a_new = mktime( 12, 0, 0, $gd_a['mon'], $gd_a['mday'], $gd_a['year'] );
    $b_new = mktime( 12, 0, 0, $gd_b['mon'], $gd_b['mday'], $gd_b['year'] );

    return round( abs( $a_new - $b_new ) / 86400 );
}

      

I saw the post Finding a date of a specific day between two PHP dates

But I didn't get my result
Please help me

+3


source to share


3 answers


Use a class DateTime

, it will be much easier:



$from_date ='01-01-2013';
$to_date ='05-01-2013';

$from_date = new DateTime($from_date);
$to_date = new DateTime($to_date);

for ($date = $from_date; $date <= $to_date; $date->modify('+1 day')) {
  echo $date->format('l') . "\n";
}

      

+4


source


$from_date ='01-01-2013';
$to_date ='05-01-2013';
$start = strtotime($from_date);
$end = strtotime($to_date);
$day = (24*60*60);
for($i=$start; $i<= $end; $i+=86400)
    echo date('l', $i);

      



+3


source


<?php
    function printDays($from, $to) {
        $from_date=strtotime($from);
        $to_date=strtotime($to);
        $current=$from_date;
        while($current<=$to_date) {
            $days[]=date('l', $current);
            $current=$current+86400;
        }

        foreach($days as $key=> $day) {
            echo $day."\n";
        }
    }
    $from_date ='01-01-2013';
    $to_date ='05-01-2013';
    printDays($from_date, $to_date);
?>

      

Loop each day between given dates (inclusive) and then add the appropriate day to the array using a function date

. Print the array and tadu! all is ready!

+1


source







All Articles