• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Time calculation between 2 dates sakthisiga Newbie Posts: 1 Threads: 1 Joined: Oct 2015 Reputation: 0 03-30-2017, 12:25 AM Hi Friends, I have 2 dates with time as below: From: 2017-03-09 09:26:00 To: 2017-03-11 09:25:00 I need to calculate exact Hour and minutes difference between 2 dates.. However I could get only 23:00 for diff functions. I need the exact hours and minutes difference.. Please help suhindra Member Posts: 71 Threads: 4 Joined: Jan 2015 Reputation: 4 03-30-2017, 02:18 AM (This post was last modified: 03-30-2017, 02:20 AM by suhindra.) PHP Code:`\$from = strtotime("2017-03-09 09:26:00");\$to = strtotime("2017-03-11 09:25:00");echo round(abs(\$to - \$from) / 60,2). " minute"; ` dave friend Posting Freak Posts: 1,020 Threads: 15 Joined: Jun 2015 Reputation: 50 03-30-2017, 07:31 AM Perhaps not the most elegant - but it works. PHP Code:`   \$start = new DateTime("2017-03-09 09:26:00");  \$end = new DateTime("2017-03-11 09:25:00");  \$interval = \$start->diff(\$end);  \$hrs = \$interval->d * 24 + \$interval->h;  echo \$hrs." hours ".\$interval->format('%i')." minutes"; ` salain Member Posts: 127 Threads: 0 Joined: Nov 2014 Reputation: 12 03-31-2017, 12:14 AM @dave friend solution can be slightly simplify PHP Code:` \$start = new DateTime("2017-03-09 09:26:00");  \$end = new DateTime("2017-03-11 09:25:00");  \$interval = \$start->diff(\$end);  echo \$interval->format('%h hours %i minutes %S seconds'); ` A good decision is based on knowledge and not on numbers. - Plato dave friend Posting Freak Posts: 1,020 Threads: 15 Joined: Jun 2015 Reputation: 50 03-31-2017, 05:05 AM (03-31-2017, 12:14 AM)salain Wrote: @dave friend solution can be slightly simplify PHP Code:` \$start = new DateTime("2017-03-09 09:26:00");  \$end = new DateTime("2017-03-11 09:25:00");  \$interval = \$start->diff(\$end);  echo \$interval->format('%h hours %i minutes %S seconds'); ` @salain, But that won't give the right answer if the interval is more than a day. Those hours must me added to the result. salain Member Posts: 127 Threads: 0 Joined: Nov 2014 Reputation: 12 03-31-2017, 06:10 AM @dave friend you are right, but your option would also be wrong if it is more than a month. A good decision is based on knowledge and not on numbers. - Plato marksman Member Posts: 92 Threads: 6 Joined: Apr 2016 Reputation: 1 03-31-2017, 06:17 AM strtotime is the best option for me God Bless CI Contributors dave friend Posting Freak Posts: 1,020 Threads: 15 Joined: Jun 2015 Reputation: 50 03-31-2017, 06:56 AM (03-31-2017, 06:10 AM)salain Wrote: @dave friend you are right, but your option would also be wrong if it is more than a month. An excellent point! This revised code handles that. PHP Code:`\$start = new DateTime("2017-03-09 09:26:00");\$end = new DateTime("2017-04-09 09:26:00");\$interval = \$start->diff(\$end);\$hrs = \$interval->days * 24 + \$interval->h;echo \$hrs." hours ".\$interval->format('%i')." minutes"; ` Narf Me Posts: 1,591 Threads: 1 Joined: Oct 2014 Reputation: 121 03-31-2017, 07:19 AM (03-31-2017, 06:17 AM)marksman Wrote: strtotime is the best option for me strtotime() is the worst thing ever.