php - For loop involving a range of dates -
i have date range in want process between each day. example between 2013-03-01 00:00:00 , 2013-04-01 00:00:00 there 31 days
so loop this
$date_next = $date_from; for($i=0;$i<31-1;$i++) { $date_next_str = new datetime($date_next); $date_next_1_str = new datetime($date_next); $date_next_1_str->modify("+1 day"); $date_next_1_str->modify("-1 second"); $date_next_1_str->modify("+1 day"); $date_next = $date_next_1_str->date; }
so in first loop 2013-03-01 00:00:00 2013-03-01 23:59:59
however when assign $date_next_1_str->date $date_next @ end of loop, $date_next still shows 2013-03-01 23:59:59, supposed 2013-03-02 00:00:00.
anyone can me this? in advance!
you can quite using php's datetime, dateinterval , dateperiod objects:-
$startdate = \datetime::createfromformat('y-m-d h:i:s', '2013-03-01 00:00:00'); $enddate = \datetime::createfromformat('y-m-d h:i:s', '2013-04-01 00:00:00'); $interval = new \dateinterval('p1d'); $enddate->add($interval); //as otherwise last day missed off. $period = new \dateperiod($startdate, $interval, $enddate); foreach($period $date){ //each $date instance of datetime var_dump($date); // or whatever want datetime object }
Comments
Post a Comment