Re: difference between two times? Date_diff and DateTime::diff

From: Date: Tue, 16 Jun 2009 13:05:20 +0000
Subject: Re: difference between two times? Date_diff and DateTime::diff
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-294080@lists.php.net to get a copy of this message
Hi Dajve and Tom, Thanks again, I totally didn't realise that this function is yet to be implemented in the mainstream PHP, So is there no function that exists in vanilla PHP that can take two dates/times and supply the difference ? If there isn't I do apologise, i've been talking to my friend who does ASP.net and he said he was sure there is for PHP. On 16 Jun 2009, at 13:11, Dajve Green wrote:
Hi Matthew, A quick note on the DateTime::diff() method - it's only available as from PHP 5.3, which is currently in release candidate stage, meaning unless you have your own server running PHP, it won't be available to use (hopefully - I would be sceptical of any webhost which rolls out RCs on production servers). If you need to know what version of PHP you're running, use: phpversion() or phpinfo()
-----Original Message----- From: Matthew Croud [mailto:matt@obviousdigital.com] Sent: 16 June 2009 12:42 To: Tom Chubb Cc: PHP General list Subject: Re: [PHP] difference between two times? Date_diff and DateTime::diff Hi Tom, Thanks for the reply, I believe I have a fair understanding of functions, and I have followed the example on the PHP manual page ( http://uk3.php.net/manual/en/function.date-diff.php ), ideally I want to know how to use the class DateTime::diff, how can I use the DateTime::diff to get the difference between two times/ dates ? I suppose then I'm after the syntax would it be like this for example: $DIfferenceInTime = DateTime::diff(10:00,12:32); Thanks again for helping me out. On 16 Jun 2009, at 12:33, Tom Chubb wrote:
Matt, Do you understand how to use functions? A function is defined like this: function () { //code goes here } You can pass arguments to be used in a function like this: function($arg1, $arg2) { //code goes here } In the first example on the following page:
http://uk3.php.net/manual/en/function.date-diff.php
To call the function you need to provide two arguments: $dtTime1 & $dtTime2 To use in a script, you need to first define the function, as per the example: <?php function GetDeltaTime($dtTime1, $dtTime2) { $nUXDate1 = strtotime($dtTime1->format("Y-m-d H:i:s")); $nUXDate2 = strtotime($dtTime2->format("Y-m-d H:i:s")); $nUXDelta = $nUXDate1 - $nUXDate2; $strDeltaTime = "" . $nUXDelta/60/60; // sec -> hour $nPos = strpos($strDeltaTime, "."); if (nPos !== false) $strDeltaTime = substr($strDeltaTime, 0, $nPos + 3); return $strDeltaTime; } ?> Then you need to call the function like this: <?php GetDeltaTime("time1-goes-here", "time2-goes-here") ?> And it should spit out the difference. Code is untested and if you didn't follow that I suggest you read up on functions: http://www.w3schools.com/php/php_functions.asp Hope this helps - I'm probably in a similar situation to you and have been dabbling with PHP for a few years just as a hobby but thought I'd try and help out. You'll learn a lot from reading this list as well. Cheers and good luck, Tom 2009/6/16 Matthew Croud <matt@obviousdigital.com> Hello, My journey of learning PHP is going well, so i've decided to make a small program which works out the difference between two times and records the data in a text file. I've searched the PHP manual for functions which can help me out, and I discovered the function Date_diff (
http://uk3.php.net/manual/en/function.date-diff.php
)and the class DateTime::diff (
http://uk3.php.net/manual/en/datetime.diff.php
) My question is, how on earth do I use these functions ? I really don't understand the manual documentation. I've just moved onto the subject of classes and so I'm fairly new to the concept, although I am following it well. If someone could explain to me how to use ether of these ( Date_diff and DateTime::diff ) I would be VERY grateful. Thank you so much! Matt -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Matthew Croud Studio Obvious Print Solutions Limited Unit 3 Abbeygate Court Stockett Lane Maidstone Kent ME15 0PP T | 0845 094 9704 F | 0845 094 9705 www.obviousprint.com
--PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Matthew Croud Studio Obvious Print Solutions Limited Unit 3 Abbeygate Court Stockett Lane Maidstone Kent ME15 0PP T | 0845 094 9704 F | 0845 094 9705 www.obviousprint.com

« previous php.general (#294080) next »