PDA

View Full Version : Php and time??


tony09uk
03-28-2012, 01: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?

cocoonfx
03-28-2012, 02:02 PM
Hi

Think you just *60

tony09uk
03-28-2012, 02: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:(

cocoonfx
03-28-2012, 02: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.

http://stackoverflow.com/questions/5177334/finding-out-difference-between-two-times

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

jmichae3
04-22-2012, 11:28 PM
<?php
$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";
?>



<?php
$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|>

http://us3.php.net/manual/en/datetime.createfromformat.php
http://us3.php.net/manual/en/function.date-create.php
http://us3.php.net/manual/en/datetimezone.construct.php
http://us3.php.net/manual/en/timezones.php
http://us3.php.net/manual/en/dateinterval.format.php
http://us3.php.net/manual/en/datetime.diff.php

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

jmichae3
04-22-2012, 11: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:
$timestamp=($hour*60*60)+($minute*60)+$second
$diff=$timestamp2-$timestamp1

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



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.
http://en.wikipedia.org/wiki/Julian_day_number
you use floor() on all your divisions.