I am working with a weather api just like Dark Sky that saves hourly and daily weather data into my database and i am trying to format the date and time.
I have this in my code
```
$weather_plot = [];
foreach ($weather_data as $day => $day_data) {
if ($day_data) {
foreach ($day_data as $hour => $data) {
if ($data) {
// Calculate day and hour
$formatted_day = date('d/m', strtotime($data->date));
$formatted_hour = str_pad($hour % 24, 2, '0', STR_PAD_LEFT);
// var_dump($data->date);
// Construct timestamp
$timestamp = strtotime($data->date . ' +' . $hour . ' hours') * 1000;
$weather_plot[] = [$timestamp, $data->temperature];
if ($min_temp === null || $max_temp === null) {
$min_temp = $max_temp = $data->temperature;
} else {
$min_temp = min($min_temp, $data->temperature);
$max_temp = max($max_temp, $data->temperature);
}
}
}
}
}
```
When i ```var_dump($data);``` i get this example data
```
public 'building_id' => int 124
public 'date' => string '2023-05-23' (length=10)
public 'hour_of_day' => int 0
public 'summary' => string 'Cloudy' (length=6)
public 'icon' => string 'cloudy' (length=6)
public 'precipIntensity' => float 0
public 'precipProbability' => float 0
public 'temperature' => float 8.74
public 'apparentTemperature' => float 7.35
public 'dewPoint' => float 7.37
public 'humidity' => float 0.91
public 'windSpeed' => float 2.45
public 'windBearing' => float 0.82
public 'visibility' => float 16.09
public 'cloudCover' => float 1
public 'pressure' => float 1007.93
public 'ozone' => float 354.72
```
and the ```hour_of_day``` goes from ```0``` to ```47```. Also the date is ```2023-05-23``` which is usually the previous date from current date.
My Question now is that how do i format the date to me ```d/m``` for each hour and also get the time in 2hrs interval like ``` 00:00, 02:00, 04:00, 06:00, 08:00, 10:00, 12:00, 14:00, 16:00, 18:00, 20:00 and 22:00```
this is how the reading is gotten from the api
```
$newday = $day . 'T00:00:00Z';
$datetime = new DateTime($newday, new DateTimeZone('UTC'));
$smile = $datetime->setTimezone(new DateTimeZone('Europe/London'));
$dd = $smile->getTimestamp();
$url = 'api.pirateweather.net/forecast/'.DARK_SKY_API_KEY.'/'.$this->latitude.','.$this->longitude.','.$dd.'?&units=si';
```
I need the year too but i want to display it in a chart as d/m displayed and also the time as seen in this as seen in this link ```ibb.co/DK6Z2sX``` instead of this ```ibb.co/BCRYQ3c```
I have this in my code
```
$weather_plot = [];
foreach ($weather_data as $day => $day_data) {
if ($day_data) {
foreach ($day_data as $hour => $data) {
if ($data) {
// Calculate day and hour
$formatted_day = date('d/m', strtotime($data->date));
$formatted_hour = str_pad($hour % 24, 2, '0', STR_PAD_LEFT);
// var_dump($data->date);
// Construct timestamp
$timestamp = strtotime($data->date . ' +' . $hour . ' hours') * 1000;
$weather_plot[] = [$timestamp, $data->temperature];
if ($min_temp === null || $max_temp === null) {
$min_temp = $max_temp = $data->temperature;
} else {
$min_temp = min($min_temp, $data->temperature);
$max_temp = max($max_temp, $data->temperature);
}
}
}
}
}
```
When i ```var_dump($data);``` i get this example data
```
public 'building_id' => int 124
public 'date' => string '2023-05-23' (length=10)
public 'hour_of_day' => int 0
public 'summary' => string 'Cloudy' (length=6)
public 'icon' => string 'cloudy' (length=6)
public 'precipIntensity' => float 0
public 'precipProbability' => float 0
public 'temperature' => float 8.74
public 'apparentTemperature' => float 7.35
public 'dewPoint' => float 7.37
public 'humidity' => float 0.91
public 'windSpeed' => float 2.45
public 'windBearing' => float 0.82
public 'visibility' => float 16.09
public 'cloudCover' => float 1
public 'pressure' => float 1007.93
public 'ozone' => float 354.72
```
and the ```hour_of_day``` goes from ```0``` to ```47```. Also the date is ```2023-05-23``` which is usually the previous date from current date.
My Question now is that how do i format the date to me ```d/m``` for each hour and also get the time in 2hrs interval like ``` 00:00, 02:00, 04:00, 06:00, 08:00, 10:00, 12:00, 14:00, 16:00, 18:00, 20:00 and 22:00```
this is how the reading is gotten from the api
```
$newday = $day . 'T00:00:00Z';
$datetime = new DateTime($newday, new DateTimeZone('UTC'));
$smile = $datetime->setTimezone(new DateTimeZone('Europe/London'));
$dd = $smile->getTimestamp();
$url = 'api.pirateweather.net/forecast/'.DARK_SKY_API_KEY.'/'.$this->latitude.','.$this->longitude.','.$dd.'?&units=si';
```
I need the year too but i want to display it in a chart as d/m displayed and also the time as seen in this as seen in this link ```ibb.co/DK6Z2sX``` instead of this ```ibb.co/BCRYQ3c```