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

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
...

Related

Weird behavior PHP DateTime and DateTimeZone

PHP DateTime and DateTimeZone works incorrectly for me around DST change dates.
I wrote a simple function to convert local time to UTC and I called it with the midnight time from the 24th of Oct to the 2nd of Nov with the timezones Europe/Paris and CET:
<?php
function timeToUTC($time, $timeZone, $format='Y-m-d H:i:sP')
{
$dt = new DateTime($time, new DateTimeZone($timeZone));
$dt->setTimeZone(new DateTimeZone('UTC'));
return $dt->format($format);
}
$localTimes = [
'2020-10-24 00:00:00',
'2020-10-25 00:00:00',
'2020-10-26 00:00:00',
'2020-10-27 00:00:00',
'2020-10-28 00:00:00',
'2020-10-29 00:00:00',
'2020-10-30 00:00:00',
'2020-10-31 00:00:00',
'2020-11-01 00:00:00',
'2020-11-02 00:00:00',
];
foreach (['Europe/Paris', 'CET'] as $timeZone) {
echo "******** $timeZone ********" . PHP_EOL;
foreach ($localTimes as $localTime) {
$utcTime = timeToUTC($localTime, $timeZone);
echo "$localTime $utcTime" . PHP_EOL;
}
}
The output:
******** Europe/Paris ********
2020-10-24 00:00:00 2020-10-23 22:00:00+00:00
2020-10-25 00:00:00 2020-10-24 22:00:00+00:00
2020-10-26 00:00:00 2020-10-25 23:00:00+00:00
2020-10-27 00:00:00 2020-10-26 23:00:00+00:00
2020-10-28 00:00:00 2020-10-27 23:00:00+00:00
2020-10-29 00:00:00 2020-10-28 23:00:00+00:00
2020-10-30 00:00:00 2020-10-29 23:00:00+00:00
2020-10-31 00:00:00 2020-10-30 23:00:00+00:00
2020-11-01 00:00:00 2020-10-31 23:00:00+00:00
2020-11-02 00:00:00 2020-11-01 23:00:00+00:00
******** CET ********
2020-10-24 00:00:00 2020-10-23 23:00:00+00:00
2020-10-25 00:00:00 2020-10-24 23:00:00+00:00
2020-10-26 00:00:00 2020-10-25 23:00:00+00:00
2020-10-27 00:00:00 2020-10-26 23:00:00+00:00
2020-10-28 00:00:00 2020-10-27 23:00:00+00:00
2020-10-29 00:00:00 2020-10-28 23:00:00+00:00
2020-10-30 00:00:00 2020-10-29 23:00:00+00:00
2020-10-31 00:00:00 2020-10-30 23:00:00+00:00
2020-11-01 00:00:00 2020-10-31 23:00:00+00:00
2020-11-02 00:00:00 2020-11-01 23:00:00+00:00
For Europe/Paris I got the changed DST instead of the 31st of October at the 26th. And even worth that there is no change at all for the timezone CET.
What do I wrong? Is it about a PHP bug?
It's about PHP on Ubuntu. I tried Ubuntu 16.04 with PHP 7.2.34-8 and Ubuntu 18.04 with 7.2.24-0.
The output is correct.
France switched to winter time October 25th, and most abbreviation timezones like CET don't have DST rules.
Countries like France are in CET 6 months out of the year, and CEST the other 6. The abbreviated timezones are confusing, so generally I just recommend people to not use them.
echo (new DateTime('2020-10-01', new DateTimeZone('Europe/Paris')))->format('T'), "\n";
echo (new DateTime('2020-11-01', new DateTimeZone('Europe/Paris')))->format('T'), "\n";
Output:
CEST
CET

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.

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.

Split DateTime interval to single days

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.

Getting first day of every week in 6 months

I'm trying to figure out how can I get the the first day of week for the last 6 months and can't get to a working solution.
If I write date("Y-m-d 00:00:00", strtotime("-1 week", date("Y-m-d")); It just subtracts 7 days from the current date; what I want to do is to always return the date of Monday from that week.
Expected result:
2011-8-8 00:00:00
2011-8-1 00:00:00
2011-7-25 00:00:00
2011-7-18 00:00:00
etc
This should do it:
for ($i=0; $i<52/2; $i++)
echo date('Y-m-d', mktime(1, 0, 0, date('m'), date('d')-date('w')-$i*7+1, date('Y'))) . " 00:00:00\n";
it's slightly changed from Mike's Post, who wants the sunday instead of the monday.
I'd recommend DateTime::createFromFormat.
Pre-PHP 5.3, you can use strtotime instead:
<?php
define('NUM_WEEKS', 10);
$dates = Array();
$dates[] = strtotime('Monday');
for ($i = 0; $i < NUM_WEEKS-1; $i++)
$dates[] = strtotime('-1 week', $dates[$i]);
foreach ($dates as $date)
echo strftime('%c', $date) . "\n";
?>
Output:
Mon Aug 22 00:00:00 2011
Mon Aug 15 00:00:00 2011
Mon Aug 8 00:00:00 2011
Mon Aug 1 00:00:00 2011
Mon Jul 25 00:00:00 2011
Mon Jul 18 00:00:00 2011
Mon Jul 11 00:00:00 2011
Mon Jul 4 00:00:00 2011
Mon Jun 27 00:00:00 2011
Mon Jun 20 00:00:00 2011
Live demo.
If you're trying to make Saturday (or any other day for that matter)
the first day of the week to select datasets, here's a good
workaround:
<?php $last_sat=date("z", strtotime("last Saturday"));
$second_last_sat=date("z", strtotime("last Saturday-1 week")); ?>
source: http://www.php.net/manual/en/function.strtotime.php
What you'd probably want is
<?php $last_mon=date("z", strtotime("last Monday "));
$second_last_mon=date("z", strtotime("last Monday-1 week")); ?>
etc..

Categories