| | |
time difference from time stamp
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2006
Posts: 82
Reputation:
Solved Threads: 0
Hi
I am trying to get the number of hours and minutes between
two time time stamps.
The expected results should be 1:59 (h:mm) instead I am getting 1:58 (h:mm).
What is wrong with my approach?
Is it also possible to use a built in php function so I don't have to
divide by 60 to convert to hours and minutes?
[php]
I am trying to get the number of hours and minutes between
two time time stamps.
The expected results should be 1:59 (h:mm) instead I am getting 1:58 (h:mm).
What is wrong with my approach?
Is it also possible to use a built in php function so I don't have to
divide by 60 to convert to hours and minutes?
[php]
PHP Syntax (Toggle Plain Text)
<? $end_time = "2008-09-05 20:59:13"; $start_time = "2008-09-05 19:00:16"; $end = date("h:i:s",strtotime($end_time)); $start = date("h:i:s",strtotime($start_time)); $diff = strtotime($end) - strtotime($start); //convert to min and sec $convert_min = $diff/60; $convert_sec = $diff % 60;//seconds //convert to hours and min $convert_hr = floor($convert_min/60);//hours $remainder = floor($convert_min % 60);//minutes $total_visit = $convert_hr.":".$remainder; ?> [/php]
•
•
Join Date: Oct 2006
Posts: 82
Reputation:
Solved Threads: 0
I am getting the correct hours and minutes by removing the seconds.
E.g. date("h:i",strtotime($end_time)).
I am still open to a more efficient way to accomplish this task.
[php]
E.g. date("h:i",strtotime($end_time)).
I am still open to a more efficient way to accomplish this task.
[php]
PHP Syntax (Toggle Plain Text)
<? $end_time = "2008-09-05 20:59:13"; $start_time = "2008-09-05 19:00:16"; $end = date("h:i",strtotime($end_time)); $start = date("h:i",strtotime($start_time)); $diff = strtotime($end) - strtotime($start); //convert to min and sec $convert_min = $diff/60; $convert_sec = $diff % 60;//seconds //convert to hours and min $convert_hr = floor($convert_min/60);//hours $remainder = floor($convert_min % 60);//minutes $total_visit = $convert_hr.":".$remainder; ?> [/php]
Check out the documentation on the date_sub function - I think this will do what you are after. Here is a link: http://au2.php.net/manual/en/function.date-sub.php
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. ![]() |
Similar Threads
- SpeedUp Your Window XP Never Than Before (Windows tips 'n' tweaks)
- Saw this a while back - so thought I'd post it lol (IT Professionals' Lounge)
- Geeky Humor (Geeks' Lounge)
- Using a switch statement with nested if/else statements (C++)
- how to make member expire after so many days with php script (PHP)
- Efficient Programming (Computer Science)
Other Threads in the PHP Forum
- Previous Thread: Reading the stored procedure OUT parameter in PHP
- Next Thread: Calling Audio Conversion Tool with PHP on Win 2003
| Thread Tools | Search this Thread |
# 5.2.10 action address apache api array auto autoincrement beginner binary broken cakephp checkbox class classes cms code cron curl database date dehasher destroy display dissertation domain dynamic echo echo$_get[x]changingitintovariable... email error errorlog fatalerror file files folder form forms function functions google href htaccess html if-else image images include insert ip javascript joomla legislation limit link load login mail masterthesis menu mlm multiple mysql mysqlquery oop open paypal pdf persist php popup problem query radio random record recursion remote script search server sessions sms sockets source space sql syntax system table tutorial update upload url validator variable video web youtube





