View Full Version : Php and time??

03-28-2012, 12:23 PM
I am trying to make a simple calculator to calculate travel times. but when it outputs the answer i get something like 1.00 minuet or 2.00 minuets which would equal 1 hour or 2 hours respectively or 0.50 minuets, which would be 30 minuets.

How do I get php to take the answer and change it to a length of time?

03-28-2012, 01:02 PM

Think you just *60

03-28-2012, 01:37 PM
I will give that a try thanks.

I did look on php.net and that shows a time() function, cant seem to work if that will work for what I want though, I couldnt make it work:(

03-28-2012, 01:41 PM
You possibly need to 60*60 the final result.

I have a formula for doing this for another program language but I don't have it to hand.

You may need several variables to get the final out come.

Don't like posting links to other forums but this looks like something similar.


03-28-2012, 03:20 PM
Well it looks like this is another stage of my development lol

04-22-2012, 10:28 PM
$date1 = date_create('2000-01-01 5:59:59 GMT-5');
$date2 = date_create('2000-01-01 5:59:59 GMT-8');
$interval = $date1->diff($date2);
echo $interval->format('%R%a days %04y-%M-%D %H:%I:%S') . "\n";

$date1 = date_create('2000-01-01 5:59:59', new DateTimeZone('America/New_York'));
$date2 = date_create('2000-01-01 5:59:59', new DateTimeZone('America/Vancouver'));
$interval = $date1->diff($date2);
echo $interval->format('%R%a days %04y-%M-%D %H:%I:%S') . "\n";

Sun 04/22/2012 15:03:10.48|C:\prj\test\php\timediff|>php datetimediff.php
+0 days %04y-00-00 03:00:00

Sun 04/22/2012 15:22:52.79|C:\prj\test\php\timediff|>


there are many ways to create a date. there is already a function to create a time diff. I hope this works.

04-22-2012, 10:45 PM
also, if you are calculating straight time, problems come when you are going over timezones, which is an offset for the given time. the timezones need to be subtracted. there is also the issue of dates, because that is also a factor, because you might be sure to wrap over a 24-hour time period when you diff time zones.

Time differences are best expressed as a +/-number of days and HH:MM:SS with no timezone (the timezones have been eliminated by subtraction).

I just got done writing a time difference calculator app.
apps like this are tough.
at the very simplest, time calculation can be converted to an integer timestamp that you can diff like this:

and the millisecond-resolution version...
$timestamp=($hour*60*60*1000)+($minute*60*1000)+($ second*1000)+$millisecond

but I suggest you use the julian day number calculation for calculating time differences, and add the gregorian epoch date offset day 1, year 0, month 1, so you get a usable number. you can find out about the julian day number calculation on wikipedia. it doesn't handle timeszones.
you use floor() on all your divisions.