Posted by: prajapatinilesh | May 7, 2009

Get all dates between two dates using php code.

<?php
$fromDate = ’01/01/2009′;
$toDate = ’01/10/2009′;

$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);

for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr = date(“Y-m-d”,$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
//print $currentDateStr.”<br />”;
}

echo  “<pre>”;
print_r($dateMonthYearArr);
echo “</pre>”;
?>

OR

function get_months($date1, $date2) {
$time1 = strtotime($date1);
$time2 = strtotime($date2);
$my = date(‘mY’, $time2);

$months = array(date(‘F Y’, $time1));

while($time1 < $time2) {
$time1 = strtotime(date(‘Y-m-d’, $time1).’ +1 month’);
if(date(‘mY’, $time1) != $my && ($time1 < $time2))
$months[] = date(‘F Y’, $time1);
}

$months[] = date(‘F Y’, $time2);
return $months;
}
echo “<pre>”; print_r(get_months(’2011-02-1′, ’2012-12-1′)); echo “</pre>”;

Thanks,
Nilesh Prajapati.


Responses

  1. Good Work

    Thank you

  2. Very useful. Your help is appreciated.

  3. Very Thnx dear! gud wrk………….

  4. Thanks ,good coding

  5. Its helpful to me… thanks

    • $(“body”).css({“display”:”none”})

  6. Thanks Dear, Nice coding.

  7. But this does take care of daylight saving problem..
    i.e this year 2009 1st November is echoed twice..(In Daylight saving time zones)

  8. I meant it does not solve the DST problem

  9. Thanx Dear……..nice code……appropriate to use…..

  10. thaks frd

  11. Thanks buddy ur answer is so useful

  12. Very useful stuff. Your help is appreciated good work.

  13. nice code

  14. works like a charm. thank you

  15. $fromDate = ’01/01/2009′;
    $toDate = ’01/10/2009′;

    Instead of the above if I give the dates in the form below

    $fromDate = ’01-01-2009′;
    $toDate = ’01-10-2009′;

    it will take d-m-Y format…and naturally gives wrong out put..but still it is helpful for me.Thank you

  16. THANKS A LOT!!!

  17. very thank

  18. Very useful stuff. Your help is appreciated good work.

  19. Thanks for the snippet. It was very helpful.

  20. Great script! Thank you for posting this.

  21. thanks for the simple code.

  22. Thanks bhai.

  23. many thanks…this help me

  24. It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function.
    Could you please suggest any other function instead of strtotime()?It is not safe.

  25. thanks a lot

  26. terima kasih(Indosesia)/Thank you………

  27. thanks nice logics:)

  28. Thanks… very useful and simple code.

  29. Just what I was looking for thank you 🙂

  30. this the best code.it is solve my proplem which cannot solve in one day.

  31. Thanks

  32. Thankyou for the posting. Very Helpful

  33. Thank You… It was very useful.

  34. hi…….. its very nice blog

  35. absolutely brilliant…keep up the good work

  36. I have found a good solution on the site below. Works like charm for me.

    http://phphelp.co/2012/03/28/how-to-print-all-the-months-and-years-between-two-dates-in-php/

    Or

    http://addr.pk/ac8b

  37. Thanks it helps me….

  38. Thanks bro………….It’s very helpful

  39. Wow … it’s a very simple script.

  40. thank you very much…………………………….

  41. thanx….its working!!!!!

  42. nice work nilesh…..

  43. Thanks Nilesh!!!


Leave a reply to Atheeswaran Cancel reply

Categories