Split DateTime interval to single days - php

I have a PHP script which receives a set of Events from a database with begin/end DateTimes, which represent working times.
Begin | End
2013-08-14 10:00:00 | 2013-08-22 09:30:00
2013-08-08 07:00:00 | 2013-08-08 15:00:00
2013-08-09 07:00:00 | 2013-08-10 07:00:00
Now I want to calculate how much has been worked each single day. For the first row I would want an output like that:
Begin | End
2013-08-14 10:00:00 | 2013-08-14 23:59:59
2013-08-15 00:00:00 | 2013-08-15 23:59:59
2013-08-16 00:00:00 | 2013-08-16 23:59:59
....
2013-08-22 00:00:00 | 2013-08-22 09:30:00
I've seen some things with DatePeriod and DateInterval, but those didn't take time into account.
Thanks for your help.

DatePeriod and DateInterval DO take time into account, so you can use those classes.
Your data :
$intervals = [
['begin' => '2013-08-14 10:00:00', 'end' => '2013-08-22 09:30:00'],
['begin' => '2013-08-08 07:00:00', 'end' => '2013-08-08 15:00:00'],
['begin' => '2013-08-09 07:00:00', 'end' => '2013-08-10 07:00:00'],
];
Quick function I wrote :
function explodePeriodByDays($begin, $end) {
$days = [];
$dayInterval = new DateInterval('P1D');
$begin = new DateTime($begin);
$end = new DateTime($end);
$_end = clone $end;
$_end->modify('+1 day');
foreach ((new DatePeriod($begin, $dayInterval, $_end)) as $i => $period) {
$_begin = $period;
if ($i) $_begin->setTime(0, 0, 0);
if ($_begin > $end) break;
$_end = clone $_begin;
$_end->setTime(23, 59, 59);
if ($end < $_end) $_end = $end;
$days[] = [
'begin' => $_begin,
'end' => $_end,
];
}
return $days;
}
Example of function use :
foreach ($intervals as $interval) {
echo "Day intervals from {$interval['begin']} to {$interval['end']} : \n";
foreach (explodePeriodByDays($interval['begin'], $interval['end']) as $day) {
echo "\t {$day['begin']->format('Y-m-d H:i:s')} | {$day['end']->format('Y-m-d H:i:s')}\n";
}
echo "\n";
}
Output of the example :
Day intervals from 2013-08-14 10:00:00 to 2013-08-22 09:30:00 :
2013-08-14 10:00:00 | 2013-08-14 23:59:59
2013-08-15 00:00:00 | 2013-08-15 23:59:59
2013-08-16 00:00:00 | 2013-08-16 23:59:59
2013-08-17 00:00:00 | 2013-08-17 23:59:59
2013-08-18 00:00:00 | 2013-08-18 23:59:59
2013-08-19 00:00:00 | 2013-08-19 23:59:59
2013-08-20 00:00:00 | 2013-08-20 23:59:59
2013-08-21 00:00:00 | 2013-08-21 23:59:59
2013-08-22 00:00:00 | 2013-08-22 09:30:00
Day intervals from 2013-08-08 07:00:00 to 2013-08-08 15:00:00 :
2013-08-08 07:00:00 | 2013-08-08 15:00:00
Day intervals from 2013-08-09 07:00:00 to 2013-08-10 07:00:00 :
2013-08-09 07:00:00 | 2013-08-09 23:59:59
2013-08-10 00:00:00 | 2013-08-10 07:00:00

If I am understanding this correctly, you want to get the difference between a begin date and an end date.
I would go about converting the time with a strtotime, subtract the timestamps, then output the date.
$begin = strtotime("2013-08-14 10:00:00");
$end = strtotime("2013-08-14 23:59:59");
$difference = ($end - $begin);
echo gmdate("H:i:s", $difference);
This would give you the time in hours, minutes, then seconds.

Related

Return Iterative Datetime between Two Datetimes for Adding Records in Laravel

Input:
$start = new DateTime('2021-09-13 06:00:00');
$end = new DateTime('2021-09-17 15:00:00');
Expected Output:
Start
End
Hours
2021-09-13 06:00:00
2021-09-13 15:00:00
9 hours
2021-09-14 06:00:00
2021-09-14 15:00:00
9 hours
2021-09-15 06:00:00
2021-09-15 15:00:00
9 hours
2021-09-16 06:00:00
2021-09-16 15:00:00
9 hours
2021-09-17 06:00:00
2021-09-17 15:00:00
9 hours
I have tried this as foundation, but not pretty sure it this would meet my expected output.
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
Log::info($dt->format("Y-m-d H:i:s \n"));
}

Is there an idea to jump Sunday date (the iteration number 7) in this code and show all dates?

I want help to show dates without Sunday(date) and show the day of( 2019-
07-27).
I want result like this : twice (Monday -> Saturday) between 2019-07-15
and 2019-07-27. this exemple is just for 2 iterations ($arrayOfMerchs)
but the number of day is 12(static) without Sunday.
Note : the date change every time. Exp :[2019-07-29 to 2019-08-10],[2019-08-12 to 2019-08-24] ...etc
If there is another way to get this code better I'm so thankful.
I want result like this:
2019-07-15 00:00:00 //Monday
2019-07-16 00:00:00
2019-07-17 00:00:00
2019-07-18 00:00:00
2019-07-19 00:00:00
2019-07-20 00:00:00 //Saturday
2019-07-22 00:00:00 //Monday
2019-07-23 00:00:00
2019-07-24 00:00:00
2019-07-25 00:00:00
2019-07-26 00:00:00
2019-07-27 00:00:00 // Saturday
2019-07-15 00:00:00 //Monday
2019-07-16 00:00:00
2019-07-17 00:00:00
2019-07-18 00:00:00
2019-07-19 00:00:00
2019-07-20 00:00:00 //Saturday
2019-07-22 00:00:00 //Monday
2019-07-23 00:00:00
2019-07-24 00:00:00
2019-07-25 00:00:00
2019-07-26 00:00:00
2019-07-27 00:00:00 //Saturday
This is my script :
<?php
$begin = new DateTime('2019-07-15');
$end = new DateTime('2019-07-27');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
$arrayOfMerchs = array(1,2);
$arrayofPlannings = array(1,2,3,4,5,6,7,8,9,10,11,12);
$cpt = 0;
foreach ($arrayOfMerchs as $merch) {
foreach ($arrayofPlannings as $planning) {
foreach ($period as $dt) {
$cpt ++;
if($cpt == 7){continue;}
echo $dt->format("Y-m-d H:i:s")."<br>";
}
break;
}
echo " "."<br>";
}
The result of this code like:
2019-07-15 00:00:00
2019-07-16 00:00:00
2019-07-17 00:00:00
2019-07-18 00:00:00
2019-07-19 00:00:00
2019-07-20 00:00:00 //
2019-07-22 00:00:00
2019-07-23 00:00:00
2019-07-24 00:00:00
2019-07-25 00:00:00
2019-07-26 00:00:00
// date of2019-07-27 00:00:00 not showing
2019-07-15 00:00:00
2019-07-16 00:00:00
2019-07-17 00:00:00
2019-07-18 00:00:00
2019-07-19 00:00:00
2019-07-20 00:00:00
2019-07-21 00:00:00 // I dont want to show Sunday
2019-07-22 00:00:00
2019-07-23 00:00:00
2019-07-24 00:00:00
2019-07-25 00:00:00
2019-07-26 00:00:00
// date of2019-07-27 00:00:00 not showing
You can do something like:
$start = new DateTime('2019-06-01');
$end = new DateTime('2019-06-30');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
$notSundayDates = [];
foreach ($period as $day) {
//check if day of the week is sunday
if ($day->format('w') !== "0") {
$notSundayDates[] = $day;
}
}
You can use date() and strtotime()
$date_to_test = "<calculated_date_time_here>";
if(date("w",strtotime($date_to_test)) == 0){
// it's a sunday so skip to the next iteration of the loop
continue;
}
You can use that when looping through the dates, you don't need H:i:s to calculate the day but it doesn't matter if it is included.

Get all array keys that has same value (date) PHP Openweathermap

I have
{"city":{"id":5128581,"name":"New York","coord":{"lon":-74.005966,"lat":40.714272},"country":"US","population":0,"sys":{"population":0}},"cod":"200","message":0.0157,"cnt":34,"list":[{"dt":1462730400,"main":{"temp":16.29,"temp_min":15.26,"temp_max":16.29,"pressure":1013.67,"sea_level":1016.94,"grnd_level":1013.67,"humidity":76,"temp_kf":1.03},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":0},"wind":{"speed":6.02,"deg":285.502},"rain":{"3h":0.005},"sys":{"pod":"d"},"dt_txt":"2016-05-08 18:00:00"},{"dt":1462741200,"main":{"temp":16.27,"temp_min":15.58,"temp_max":16.27,"pressure":1016.17,"sea_level":1019.42,"grnd_level":1016.17,"humidity":49,"temp_kf":0.69},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":6.3,"deg":299.501},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-08 21:00:00"},{"dt":1462752000,"main":{"temp":14.75,"temp_min":14.4,"temp_max":14.75,"pressure":1019.5,"sea_level":1022.93,"grnd_level":1019.5,"humidity":48,"temp_kf":0.34},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":56},"wind":{"speed":5.35,"deg":281.001},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-09 00:00:00"},{"dt":1462762800,"main":{"temp":13.13,"temp_min":13.13,"temp_max":13.13,"pressure":1022.28,"sea_level":1025.77,"grnd_level":1022.28,"humidity":52,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":4.76,"deg":277.503},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-09 03:00:00"},{"dt":1462773600,"main":{"temp":11.86,"temp_min":11.86,"temp_max":11.86,"pressure":1023.68,"sea_level":1027.06,"grnd_level":1023.68,"humidity":56,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":4.07,"deg":276.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-09 06:00:00"},{"dt":1462784400,"main":{"temp":10.11,"temp_min":10.11,"temp_max":10.11,"pressure":1024.81,"sea_level":1028.24,"grnd_level":1024.81,"humidity":66,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":4.01,"deg":248.007},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-09 09:00:00"},{"dt":1462795200,"main":{"temp":12.37,"temp_min":12.37,"temp_max":12.37,"pressure":1025.97,"sea_level":1029.33,"grnd_level":1025.97,"humidity":58,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":3.56,"deg":244.001},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-09 12:00:00"},{"dt":1462806000,"main":{"temp":17.12,"temp_min":17.12,"temp_max":17.12,"pressure":1025.94,"sea_level":1029.3,"grnd_level":1025.94,"humidity":47,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":5.28,"deg":254.505},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-09 15:00:00"},{"dt":1462816800,"main":{"temp":19.47,"temp_min":19.47,"temp_max":19.47,"pressure":1025.22,"sea_level":1028.61,"grnd_level":1025.22,"humidity":40,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":5.7,"deg":267.503},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-09 18:00:00"},{"dt":1462827600,"main":{"temp":20.43,"temp_min":20.43,"temp_max":20.43,"pressure":1024.61,"sea_level":1028.02,"grnd_level":1024.61,"humidity":36,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":5.41,"deg":268.506},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-09 21:00:00"},{"dt":1462838400,"main":{"temp":18.92,"temp_min":18.92,"temp_max":18.92,"pressure":1025.5,"sea_level":1028.87,"grnd_level":1025.5,"humidity":37,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":4.71,"deg":267},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-10 00:00:00"},{"dt":1462849200,"main":{"temp":15.24,"temp_min":15.24,"temp_max":15.24,"pressure":1027.67,"sea_level":1031.12,"grnd_level":1027.67,"humidity":45,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":3.01,"deg":320.504},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-10 03:00:00"},{"dt":1462860000,"main":{"temp":12.42,"temp_min":12.42,"temp_max":12.42,"pressure":1029.87,"sea_level":1033.33,"grnd_level":1029.87,"humidity":50,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":2.87,"deg":0.5},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-10 06:00:00"},{"dt":1462870800,"main":{"temp":8.48,"temp_min":8.48,"temp_max":8.48,"pressure":1031.42,"sea_level":1034.85,"grnd_level":1031.42,"humidity":64,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"02n"}],"clouds":{"all":8},"wind":{"speed":2.21,"deg":19.5059},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-10 09:00:00"},{"dt":1462881600,"main":{"temp":12,"temp_min":12,"temp_max":12,"pressure":1032.33,"sea_level":1035.75,"grnd_level":1032.33,"humidity":53,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":1.3,"deg":46.001},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-10 12:00:00"},{"dt":1462892400,"main":{"temp":15.65,"temp_min":15.65,"temp_max":15.65,"pressure":1032.61,"sea_level":1036.02,"grnd_level":1032.61,"humidity":44,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":20},"wind":{"speed":1.46,"deg":178.503},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-10 15:00:00"},{"dt":1462903200,"main":{"temp":15.97,"temp_min":15.97,"temp_max":15.97,"pressure":1031.52,"sea_level":1034.7,"grnd_level":1031.52,"humidity":44,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":76},"wind":{"speed":3.12,"deg":221.001},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-10 18:00:00"},{"dt":1462914000,"main":{"temp":15.47,"temp_min":15.47,"temp_max":15.47,"pressure":1030,"sea_level":1033.18,"grnd_level":1030,"humidity":48,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":92},"wind":{"speed":3.39,"deg":220.502},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-10 21:00:00"},{"dt":1462924800,"main":{"temp":14.45,"temp_min":14.45,"temp_max":14.45,"pressure":1029.57,"sea_level":1032.9,"grnd_level":1029.57,"humidity":55,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":92},"wind":{"speed":3.42,"deg":223.001},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-11 00:00:00"},{"dt":1462935600,"main":{"temp":14.05,"temp_min":14.05,"temp_max":14.05,"pressure":1029.6,"sea_level":1032.97,"grnd_level":1029.6,"humidity":64,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":92},"wind":{"speed":2.61,"deg":233.504},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-11 03:00:00"},{"dt":1462946400,"main":{"temp":12.98,"temp_min":12.98,"temp_max":12.98,"pressure":1029.21,"sea_level":1032.63,"grnd_level":1029.21,"humidity":72,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":92},"wind":{"speed":2.15,"deg":254.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-11 06:00:00"},{"dt":1462957200,"main":{"temp":11.62,"temp_min":11.62,"temp_max":11.62,"pressure":1029.25,"sea_level":1032.71,"grnd_level":1029.25,"humidity":80,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":92},"wind":{"speed":0.98,"deg":273.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-11 09:00:00"},{"dt":1462968000,"main":{"temp":13.73,"temp_min":13.73,"temp_max":13.73,"pressure":1030.19,"sea_level":1033.67,"grnd_level":1030.19,"humidity":65,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":92},"wind":{"speed":1.21,"deg":352},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-11 12:00:00"},{"dt":1462978800,"main":{"temp":16.82,"temp_min":16.82,"temp_max":16.82,"pressure":1030.72,"sea_level":1034.15,"grnd_level":1030.72,"humidity":45,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":92},"wind":{"speed":1.56,"deg":63.0032},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-11 15:00:00"},{"dt":1462989600,"main":{"temp":18.75,"temp_min":18.75,"temp_max":18.75,"pressure":1029.77,"sea_level":1033.18,"grnd_level":1029.77,"humidity":42,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":1.56,"deg":114.005},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-11 18:00:00"},{"dt":1463000400,"main":{"temp":18.92,"temp_min":18.92,"temp_max":18.92,"pressure":1028.57,"sea_level":1032,"grnd_level":1028.57,"humidity":40,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":20},"wind":{"speed":1.76,"deg":158.501},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-11 21:00:00"},{"dt":1463011200,"main":{"temp":15.8,"temp_min":15.8,"temp_max":15.8,"pressure":1029.06,"sea_level":1032.42,"grnd_level":1029.06,"humidity":47,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":20},"wind":{"speed":2.32,"deg":171.5},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-12 00:00:00"},{"dt":1463022000,"main":{"temp":10.9,"temp_min":10.9,"temp_max":10.9,"pressure":1030.34,"sea_level":1033.69,"grnd_level":1030.34,"humidity":71,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":36},"wind":{"speed":1.16,"deg":156},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-12 03:00:00"},{"dt":1463032800,"main":{"temp":10.16,"temp_min":10.16,"temp_max":10.16,"pressure":1030.41,"sea_level":1033.88,"grnd_level":1030.41,"humidity":81,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":80},"wind":{"speed":1.06,"deg":88.5057},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-12 06:00:00"},{"dt":1463043600,"main":{"temp":10.2,"temp_min":10.2,"temp_max":10.2,"pressure":1030.3,"sea_level":1033.76,"grnd_level":1030.3,"humidity":82,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":76},"wind":{"speed":1.4,"deg":55.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-05-12 09:00:00"},{"dt":1463054400,"main":{"temp":12.33,"temp_min":12.33,"temp_max":12.33,"pressure":1030.4,"sea_level":1033.92,"grnd_level":1030.4,"humidity":72,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":76},"wind":{"speed":2.02,"deg":57.5028},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-12 12:00:00"},{"dt":1463065200,"main":{"temp":16.46,"temp_min":16.46,"temp_max":16.46,"pressure":1030.83,"sea_level":1034.2,"grnd_level":1030.83,"humidity":53,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":48},"wind":{"speed":2.32,"deg":75.5036},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-12 15:00:00"},{"dt":1463076000,"main":{"temp":18.67,"temp_min":18.67,"temp_max":18.67,"pressure":1030.02,"sea_level":1033.43,"grnd_level":1030.02,"humidity":43,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":36},"wind":{"speed":2.56,"deg":110.51},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-12 18:00:00"},{"dt":1463086800,"main":{"temp":17.58,"temp_min":17.58,"temp_max":17.58,"pressure":1028.71,"sea_level":1032.23,"grnd_level":1028.71,"humidity":44,"temp_kf":0},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":92},"wind":{"speed":2.77,"deg":115.504},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-05-12 21:00:00"}]}
And here's my code
$data = file_get_contents('http://api.openweathermap.org/data/2.5/forecast?q=HauGiang,vi&appid=307f2a5a7c6940057fcde7ccbb47c623&units=metric');
$json = json_decode($data);
foreach ($json->list as $myday){
echo "Time: ".date($myday->dt_txt);
echo '</br>';
}
Desire Output:
I want to get all same value has same day (without time) to insert into div, so how can i do that? Sorry for my english . Thank you so much !
$json = json_decode($data);
$arr = array();
foreach ($json->list as $myday){
$date = date("d", strtotime($myday->dt_txt));
$arr[$date][] = date("Y-m-d H:00:00", strtotime($myday->dt_txt));
}
foreach($arr as $value){
echo '<div>';
foreach($value as $val){
echo 'Time:'. $val;
}
echo '</div><hr/>';
}
Result:
Time:2016-05-08 18:00:00
Time:2016-05-08 21:00:00
------------------------
Time:2016-05-09 00:00:00
Time:2016-05-09 03:00:00
Time:2016-05-09 06:00:00
Time:2016-05-09 09:00:00
Time:2016-05-09 12:00:00
Time:2016-05-09 15:00:00
Time:2016-05-09 18:00:00
Time:2016-05-09 21:00:00
------------------------
Time:2016-05-10 00:00:00
Time:2016-05-10 03:00:00
Time:2016-05-10 06:00:00
Time:2016-05-10 09:00:00
Time:2016-05-10 12:00:00
Time:2016-05-10 15:00:00
Time:2016-05-10 18:00:00
Time:2016-05-10 21:00:00
------------------------
Time:2016-05-11 00:00:00
Time:2016-05-11 03:00:00
Time:2016-05-11 06:00:00
Time:2016-05-11 09:00:00
Time:2016-05-11 12:00:00
Time:2016-05-11 15:00:00
Time:2016-05-11 18:00:00
Time:2016-05-11 21:00:00
------------------------
Time:2016-05-12 00:00:00
Time:2016-05-12 03:00:00
Time:2016-05-12 06:00:00
Time:2016-05-12 09:00:00
Time:2016-05-12 12:00:00
Time:2016-05-12 15:00:00
Time:2016-05-12 18:00:00
Time:2016-05-12 21:00:00
$data = file_get_contents('http://api.openweathermap.org/data/2.5/forecast?q=HauGiang,vi&appid=307f2a5a7c6940057fcde7ccbb47c623&units=metric');
$json = json_decode($data);
$all_dates = array();
foreach ($json->list as $myday){
$only_date = date('Y-m-d', strtotime($myday->dt_txt));
$all_dates[$only_date][] = $myday;
}
foreach($all_dates as $only_date => $mydays){
echo "<div>";
echo "<h1>Date: {$only_date}</h1>";
foreach($mydays as $myday){
echo $myday->dt_txt."<br>";
}
echo "</div>";
}
Output:
Date: 2016-05-08
2016-05-08 18:00:00
2016-05-08 21:00:00
Date: 2016-05-09
2016-05-09 00:00:00
2016-05-09 03:00:00
2016-05-09 06:00:00
2016-05-09 09:00:00
2016-05-09 12:00:00
2016-05-09 15:00:00
2016-05-09 18:00:00
2016-05-09 21:00:00
...

Fatal error: Maximum execution time of 120 seconds exceeded

Good day everyone, I'm trying . This is the code which demonstrates what I'm doing:
<?php
$NoOfGames = 10;
$time = array("06:00:00 " => "07:00:00", "07:00:00" => "08:00:00", "08:00:00" => "09:00:00", "09:00:00" => "10:00:00", "10:00:00" => "11:00:00", "11:00:00" => "12:00:00","12:00:00" => "13:00:00","13:00:00" => "14:00:00","14:00:00" => "15:00:00","15:00:00" => "16:00:00","16:00:00" => "17:00:00","17:00:00" => "18:00:00");
for($i=0;$i<$NoOfGames;$i+1){
$start_time = array_rand($time);
$end_time = $time[$start_time];
$time_new[$start_time] = $end_time;
}
$i =1;
foreach($time_new as $start => $end)
{
echo $i. ") ". $start . " to ". $end . "<br>";
$i++;
}
?>
However, this outputs
and I want to display it like this instead
1) 17:00:00 to 18:00:00
2) 06:00:00 to 07:00:00
3) 12:00:00 to 13:00:00
4) 11:00:00 to 12:00:00
5) 16:00:00 to 17:00:00
6) 08:00:00 to 09:00:00
7) 13:00:00 to 14:00:00
8) 15:00:00 to 16:00:00
9) 14:00:00 to 15:00:00
10) 09:00:00 to 10:00:00
This should work:
Change $i+1 to $i++
The reason is that $i+1 just evaluates to 1 every time (ie doesn't actually increment $i), meaning the loop never exits.

PHP relative month not producing expected result [duplicate]

This question already has answers here:
Removing exactly one month from a date
(2 answers)
Closed 8 years ago.
So, I am working with subtracting relative months. According to the documentation:
Relative month values are calculated based on the length of months that they pass through. An example would be "+2 month 2011-11-30", which would produce "2012-01-30". This is due to November being 30 days in length, and December being 31 days in length, producing a total of 61 days.
I understand why the example when adding months, but I don't understand why 2014-12-31 -1 month produces 2014-12-01. Its 31 days in December, shouldn't the result be the last of November?
Example code. Function 1 produces the result I was expecting from function 2 and 3.
//Func 1
$date = '2014-12-31 23:59:59'; //YYYY-MM-DD
$days = 0;
echo $date.'<br>';
for ($i = 1; $i < 13; $i++) {
$days += cal_days_in_month(CAL_GREGORIAN, (13-$i), 2014);
echo date("Y-m-d H:i:s", strtotime("-$days days", strtotime($date))).'<br>';
}
echo "<br><br>";
//Func 2
$date = '2014-12-31 23:59:59'; //YYYY-MM-DD
echo $date.'<br>';
for ($i = 1; $i < 13; $i++) {
echo date("Y-m-d H:i:s", strtotime("-$i months", strtotime($date))).'<br>';
}
//Func 3
echo "<br><br>";
$dateObj = new DateTime("2014-12-31 23:59:59");
echo $dateObj->format("Y-m-d H:i:s").'<br>';
$dateIntervalObj = new DateInterval("P1M");
for ($i = 0; $i < 12; $i++) {
echo $dateObj->format("Y-m-d H:i:s").'<br>';
$dateObj->sub($dateIntervalObj);
}
Result:
2014-12-31 23:59:59
2014-11-30 23:59:59
2014-10-31 23:59:59
2014-09-30 23:59:59
2014-08-31 23:59:59
2014-07-31 23:59:59
2014-06-30 23:59:59
2014-05-31 23:59:59
2014-04-30 23:59:59
2014-03-31 23:59:59
2014-02-28 23:59:59
2014-01-31 23:59:59
2013-12-31 23:59:59
2014-12-31 23:59:59
2014-12-01 23:59:59
2014-10-31 23:59:59
2014-10-01 23:59:59
2014-08-31 23:59:59
2014-07-31 23:59:59
2014-07-01 23:59:59
2014-05-31 23:59:59
2014-05-01 23:59:59
2014-03-31 23:59:59
2014-03-03 23:59:59
2014-01-31 23:59:59
2013-12-31 23:59:59
2014-12-31 23:59:59
2014-12-31 23:59:59
2014-12-01 23:59:59
2014-11-01 23:59:59
2014-10-01 23:59:59
2014-09-01 23:59:59
2014-08-01 23:59:59
2014-07-01 23:59:59
2014-06-01 23:59:59
2014-05-01 23:59:59
2014-04-01 23:59:59
2014-03-01 23:59:59
2014-02-01 23:59:59
seems to always round up:
2014-10-31 +1 month => 2014-12-01 // this makes sense
2014-12-31 -1 month => 2014-12-01 // this doesn't
in both cases if the day does not exist, it uses the day after it.

Categories