How do I check hours between two date/times ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I check hours between two date/times ?

Post by simonmlewis »

[text]23 May 2014 12:30:42 [/text]
'datetime' field.
Records it as:

Code: Select all

$entrydate = date('Y-m-d H:i:s');
So how do I say: $hours = $todaydate - $entrydate?
Bearing in mind I do want to calculate it down to hours, and not just days.

It's so we can spot if a message has not been replied to for 48 hours.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I check hours between two date/times ?

Post by Celauran »

DateTime::diff

Code: Select all

php > $start_date = new DateTime('2014-05-28 10:00:00');
php > $end_date = new DateTime('2014-05-29 11:30:00');
php > $interval = $end_date->diff($start_date);
php > print_r($interval);
DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 1
    [h] => 1
    [i] => 30
    [s] => 0
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 1
    [days] => 1
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check hours between two date/times ?

Post by simonmlewis »

So how do I extract just the hours number from that? And use it in a variable query?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I check hours between two date/times ?

Post by Celauran »

It's a property of the object that you can access directly. You'll also need to convert days to hours.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check hours between two date/times ?

Post by simonmlewis »

Ok well I don't know how to access what you are suggesting - this is why I am asking.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I check hours between two date/times ?

Post by Celauran »

There's a print_r of the object above.

Code: Select all

$hours = (24 * $interval->d) + $interval->h;
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I check hours between two date/times ?

Post by Celauran »

Note that that will break once you start getting into intervals of months or years. You could use the same approach to write something more robust pretty easily should the need arise.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check hours between two date/times ?

Post by simonmlewis »

I'm trying to get it basically just showing the hours between each message now.

Code: Select all

$query = "SELECT subject, message, firstname, lastname FROM tickets WHERE datetrashed IS NULL AND dateclosed IS NULL AND dateupdated IS NULL";
 $result = $pdo->query($query);
    while ($row = $result->fetch(PDO::FETCH_OBJ)) 
      {   
$start_date = new DateTime('$row->dateupdated');
php > $end_date = new DateTime('$todaydate');
php > $interval = $end_date->diff($start_date);
php > print_r($interval);
DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 1
    [h] => 1
    [i] => 30
    [s] => 0
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 1
    [days] => 1
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)
$hours = (24 * $interval->d) + $interval->h;
echo "$row->firstname $hours<br/>";
}
[text]Parse error: syntax error, unexpected 'Object' (T_STRING) in C:\xampp\phpMyAdmin\site\includes\a_tickets.inc on line 30[/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I check hours between two date/times ?

Post by Celauran »

You can't just paste console output directly into your code. Try this.

Code: Select all

$query = "SELECT subject, message, firstname, lastname FROM tickets WHERE datetrashed IS NULL AND dateclosed IS NULL AND dateupdated IS NULL";
$result = $pdo->query($query);
while ($row = $result->fetch(PDO::FETCH_OBJ)) {
	$start_date = new DateTime($row->dateupdated);
	$end_date = new DateTime();
	$interval = $end_date->diff($start_date);
	$hours = (24 * $interval->d) + $interval->h;
	echo "$row->firstname $hours<br/>";
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check hours between two date/times ?

Post by simonmlewis »

Firstly, my bad. firstname is not in that DB table, however, with the below, it shows just 0, where one of them should be at least 2 hours.

Code: Select all

$query = "SELECT subject, ticket, dateupdated FROM tickets WHERE datetrashed IS NULL AND dateclosed IS NULL AND dateupdated IS NULL";
$result = $pdo->query($query);
while ($row = $result->fetch(PDO::FETCH_OBJ)) {
        $start_date = new DateTime($row->dateupdated);
        $end_date = new DateTime();
        $interval = $end_date->diff($start_date);
        $hours = (24 * $interval->d) + $interval->h;
        echo "$row->ticket $hours<br/>";
}
[text]26064 0
27584 0
25301 0
10986 0
9223 0[/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I check hours between two date/times ?

Post by Celauran »

Have you checked that the DateTime objects are correct? I don't know what your input is. Have you inspected the DateTimeInterval object? Does it look OK?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I check hours between two date/times ?

Post by Celauran »

Oh well no wonder!
... AND dateupdated IS NULL
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check hours between two date/times ?

Post by simonmlewis »

Idiot!!!

Ok, I've changed that to be dateupdated IS NOT NULL AND updatedby = 'customer'.
But this goes back to a problem I have raised elsewhere here.

I want to know what the LAST entry of a ticket number is, and when it is updatedby = 'customer'.

so if you have:

Ticket: 123 ID: 5 Updatedby: customer
Ticket: 123 ID: 6 Updatedby: admin

I don't want that to find anything, because 'admin' has replied.

But if it was like this:
Ticket: 123 ID: 5 Updatedby: customer
Ticket: 123 ID: 6 Updatedby: admin
Ticket: 123 ID: 7 Updatedby: customer

Yes, I want ID:7 to be spotted.

How?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I check hours between two date/times ?

Post by Celauran »

I'd select the MAX(id), group by ticket, and check updatedby afterward in PHP. Seems easiest.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check hours between two date/times ?

Post by simonmlewis »

But then that is going to find the wrong entry. For example, if it finds $row->updatedby to be "admin", but the last one is actually "customer".... echoing "$row->updatedby" will produce "admin" incorrectly..............

Why can it not say "find the max entry for ticketid, ONLY if it equals "customer" ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply