logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 03-28-2012, 12:23 PM   #1
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default Php and time??

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?
tony09uk is offline   Reply With Quote
Old 03-28-2012, 01:02 PM   #2
cocoonfx
cocoonfx's Avatar
 
Join Date: Nov 2006
Location: Tamworth,UK
Posts: 576
Default

Hi

Think you just *60
cocoonfx is offline   Reply With Quote
Old 03-28-2012, 01:37 PM   #3
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default

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
tony09uk is offline   Reply With Quote
Old 03-28-2012, 01:41 PM   #4
cocoonfx
cocoonfx's Avatar
 
Join Date: Nov 2006
Location: Tamworth,UK
Posts: 576
Default

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/5...ween-two-times
cocoonfx is offline   Reply With Quote
Old 03-28-2012, 03:20 PM   #5
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default

Well it looks like this is another stage of my development lol
tony09uk is offline   Reply With Quote
Old 04-22-2012, 10:28 PM   #6
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

PHP Code:
<?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 Code:
<?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/datetim...fromformat.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 is offline   Reply With Quote
Old 04-22-2012, 10:45 PM   #7
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

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:
PHP Code:
$timestamp=($hour*60*60)+($minute*60)+$second
 $diff
=$timestamp2-$timestamp1 
and the millisecond-resolution version...
PHP Code:
$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.
jmichae3 is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:45 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com