Working with Date and Time in PHP

henryeti

New member
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```
 
To format the date as "d/m" and get the time in 2-hour intervals, you can modify your code as follows:

phpCopy code
$weather_plot = [];
foreach ($weather_data as $day => $day_data) {
if ($day_data) {
foreach ($day_data as $hour => $data) {
if ($data) {
// Calculate day, month, and hour
$formatted_day = date('d/m', strtotime($data->date));
$formatted_hour = str_pad($hour * 2, 2, '0', STR_PAD_LEFT).':00';

// Construct timestamp
$timestamp = strtotime($data->date . ' +' . ($hour * 2) . ' 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);
}
}
}
}
}

In this code, I changed the calculation of the formatted hour to multiply the $hour value by 2 to get the 2-hour interval. Then, I appended ':00' to display the hour in the desired format.

Now, when you access $formatted_day and $formatted_hour in your charting code, you should have the date in "d/m" format and the time in 2-hour intervals like "00:00", "02:00", "04:00", and so on.

Regarding displaying the year in the chart, if you want to include the year in the date format, you can modify the $formatted_day line to include the year as well:

phpCopy code
$formatted_day = date('d/m/Y', strtotime($data->date));

This will give you the date in the format "d/m/Y" (e.g., "23/05/2023") with the year included.

Remember to update your charting code to use the modified variables $formatted_day and $formatted_hour in the desired locations to achieve the desired display.
 
Back
Top