Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

henryeti's avatar

Working with Date and Time in PHP

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

0 likes
6 replies
Snapey's avatar

so you have a reading every thirty minutes?

If you want two-hour readings, how will you consolidate the four readings into one?

How come you only need day and month, not year? You cannot have a date that has no year -thats just a string

henryeti's avatar

@Snapey 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

Snapey's avatar

@henryeti dont confuse the chart axis labelling with the number of points of data

you will notice that the chart has data points between the labels

henryeti's avatar

@Snapey yes it does... but not sure why the date and time are different. If i var_dump the $timestamp from the first code i get 48hrs time but if i should change the code to $timestamp = strtotime($day . ' ' . str_pad($hour, 2, '0', STR_PAD_LEFT) . ':00:00 UTC') * 1000; but the chart keeps returning to 01/23 and the time is always 00:00

Snapey's avatar

@henryeti You just need an array of the date/time and the value.

Configure the chart to render the x-axis labels according to your requirements.

You are over-thinking this

1 like
henryeti's avatar

@Snapey Apparently the weather api gets 48hrs data but i added a check to only get 24hrs like this

foreach ($weather_data as $day => $day_data) {
					if ($day_data) {
						foreach ($day_data as $hour => $data) {
							if ($data) {  // Only consider the first 12 hours
				
								if ($hour < 24) { // Check if the hour is within the first 24 hours
									$timestamp = strtotime($day . ' ' . str_pad($hour, 2, '0', STR_PAD_LEFT) . ':00:00 UTC') * 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);
									}
								}
							}
						}
					}
				}

and it works

Please or to participate in this conversation.