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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Wonderful, thanks Ian for your function, and also thank you Tom for having a head scratch on my behalf ;)



On 16 Jun 2009, at 16:20, Ian wrote:

On 16 Jun 2009 at 14:05, Matthew Croud wrote:

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.

Hi,

This is a quick function that works using unix time stamps. Its a bit quick and messy but works ok. Take a look at the manual page for the strtotime() function to get a better idea
of what it can handle.

<?

$Date1_UnixTimeStamp=strtotime("10:10:45");
$Date2_UnixTimeStamp=strtotime("12:45:03");

print_r(DateDiff( $Date1_UnixTimeStamp, $Date2_UnixTimeStamp));

function DateDiff( $Date1_UnixTimeStamp, $Date2_UnixTimeStamp){
	
	$return_difference	= Array(
		"Days"		=>	0,
		"Hours"		=>	0,
		"Minutes"	=>	0,
		"Seconds"	=>	0
	);
	
	$amounts	= Array(
		"Days"		=>	60*60*24,
		"Hours"		=>	60*60,
		"Minutes"	=>	60
	);
	
	$difference = $Date2_UnixTimeStamp - $Date1_UnixTimeStamp;
	
	if($difference >= $amounts["Days"]){
		$return_difference["Days"] = floor($difference / $amounts["Days"]);
		$difference -=  $return_difference["Days"] * $amounts["Days"];
	}
	
	if($difference >= $amounts["Hours"]){
$return_difference["Hours"] = floor($difference / $amounts["Hours"]);
		$difference -=   $return_difference["Hours"] * $amounts["Hours"];
	}
	if($difference >= $amounts["Minutes"]){
$return_difference["Minutes"] = floor($difference / $amounts["Minutes"]);
		$difference -=  $return_difference["Minutes"] * $amounts["Minutes"];
	}
	
	$return_difference["Seconds"] = $difference;
	
	return $return_difference;
}

?>

Regards

Ian
--



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@xxxxxxxxxxxxxxxxxx]
Sent: 16 June 2009 12:42
To: Tom Chubb
Cc: PHP General list
Subject: Re:  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@xxxxxxxxxxxxxxxxxx>

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









--
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






[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux