Chaim Friedman
Chaim Friedman

Reputation: 720

Go over 24 hours in a date?

I am working on project (a Google Transit feed) where I am required to provide the times for each stop on a bus route in the following common format: 21:00:00 and so forth.

Problem is, if times continue past midnight for a given trip, they require it to continue the hour counting accordingly. They explain quite specifically that 02:00:00 should become 26:00:00 and 03:45:00 should become 27:45:00 etc.

I am baffled on how to display such with any of the date() or strtotime() functions.

The only thing I can think of in my particular situation would be to function match and replace any strings in my output between 00:00:00 and 04:00:00, as that would clearly mean (again, for me only) that these are trips originating before midnight, but I don't feel that's the correct way.

Upvotes: 1

Views: 1828

Answers (2)

Death By Penguin
Death By Penguin

Reputation: 111

Well seeing as it's only displaying on the page, you can

firstly get your date from where ever Let's say $date = 00:00:00

$exploded_date = explode(":", $date);

This takes $date and puts it into an array so

$exploded_date[0] is hh

$exploded_date[1] is mm

$exploded_date[2] is ss

Then what you can do is use ltrim() to remove the leading 0 from 00 to 04 $exploded_date[0] - This makes it comparable in the if statement I'll do after

if($exploded_date[0] <= 4) {

    $exploded_date[0] = ltrim($exploded_date[0], "0");

    $exploded_date[0] = $exploded_date[0]+24;
}

Then you can implode the array back together into one string

$date = implode(":", $exploded_date);

// if the hour is 00 to 04 it will come out as 24 to 28
// e.g. 24:35:30
echo $date;

Despite giving you an answer. It's a silly thing to be doing, but it's not your choice so here you go :)

Upvotes: 3

giorgio
giorgio

Reputation: 10202

The way you display something doesn't necesarily has to be the same way you store something.

I don't know how you calculate the times, but assuming you have a start date and time, and some interval, you could calculate the end time as follows:

date_default_timezone_set('Europe/London');

$start_datetime = new DateTime('2014-11-11T21:00:00');
$next_stop = new DateTime('2014-11-12T02:00:00');

echo $start_datetime->format('Y-m-d H:i'); // 2014-11-11 21:00
echo $next_stop->format('Y-m-d H:i'); // 2014-11-12 02:00

$interval = $start_datetime->diff($next_stop);

// display next stop: 2014-11-11 26:00
echo ($start_datetime->format('Y') + $interval->y) .'-'
     . ($start_datetime->format('m') + $interval->m) .'-'
     . ($start_datetime->format('d') + $interval->d) .' '
     . ($start_datetime->format('H') + $interval->h) .':'
     . ($start_datetime->format('i') + $interval->i);

What I'm doing: create the start date (& time) and the datetime of the next stop. With the DateTime::diff() function I'm calculating the difference, and then, only for display (!) I add up each year, month, day, hour and minute to the datetime year, month etc. of the next stop.

This way you can still store your dates and times in a way every human being and computer system will understand (because let's be honest; to represent a time as 27:45 PM is quite ridiculous...)

I don't know if you only want the hours to be added up and roll over the 24 hour, or also days in a month etc. It's up to you how you handle these cases. Good luck!

Upvotes: 1

Related Questions