Teraz chciałby z takiego przedziału uzyskać dwa dla tych dni czyli
1. 2015-11-22 22:20:15 -> 2015-11-22 23:59:59
2. 2015-11-23 00:00:00 -> 2015-11-23 02:13:11
Wyciągnięcie z mojego przedziału dat to nic trudnego i w sieci jest masa przykładów, ale nie mogę nigdzie znaleźć czegoś takiego dla czasu.
Jedyne co mi się udało znaleźć to to:
$start_date = '27:04:2013'; $start_time = '16:30'; $end_date = '29:04:2013'; $end_time = '22:30'; // Date input strings and generate a suitable DatePeriod $start = DateTime::createFromFormat("d:m:Y H:i", "$start_date $start_time"); $end = DateTime::createFromFormat("d:m:Y H:i", "$end_date $end_time"); $interval = new DateInterval('P1D'); $period = new DatePeriod($start, $interval, $end); foreach ($period as $date) { // Get midnight at start of current day $date_start = clone $date; $date_start->modify('midnight'); // Get 23:59:59, end of current day // (moving to midnight of next day might be good too) $date_end = clone $date; $date_end->modify('23:59:59'); // Take care of partial days // Here you would construct your array of // DateTime pairs, or DateIntervals, as you want. "%s -> %s \n", $date_start->format('Y-m-d H:i'), $date_end->format('Y-m-d H:i') ); }
ale niestety to działa prawidłowo tylko jeśli w przedziale minęło więcej niż 24h
