Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get day names between two date in PHP

Tags:

php

How to get name of days between two dates in PHP?

Input:

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

Output:

Tuesday
Wednesday
Thursday
Friday
Saturday

Tried 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 date of particular day between two dates PHP

But I don't got my result
Please help me

like image 437
Nandu Avatar asked Feb 10 '26 01:02

Nandu


1 Answers

Use the DateTime class, it will be much more simple:

$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";
}
like image 176
xdazz Avatar answered Feb 12 '26 16:02

xdazz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!