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
<?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 to share