[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

date subtraction php

Posted on 2014-08-15
13
Medium Priority
?
186 Views
Last Modified: 2014-08-15
Hi,

I have a date in the past in format mm/dd/yy hh:mm, like 08/01/14 03:21 or 08/07/14 23:51, i want to substract this from now() and obtain how many hours and minutes are in a format in hh:mm like10:30 or 132:43, any idea?
0
Comment
Question by:joyacv2
  • 6
  • 4
  • 3
13 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40263294
Either you have a date typed variable , or you have a string variable that holds a text that can be parsed into a date given that format.
Check out str_to_date function.
0
 
LVL 1

Author Comment

by:joyacv2
ID: 40263314
Hi,

How is that?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40263316
http://php.net/manual/en/datetime.diff.php
To learn about date time difference function
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:joyacv2
ID: 40263325
i read that, but don't understand
0
 
LVL 58

Expert Comment

by:Gary
ID: 40263355
What format is that date in ?
Is it dd/mm/yyyy or mm/dd/yyyy
0
 
LVL 1

Author Comment

by:joyacv2
ID: 40263390
Hi Gary

 mm/dd/yyyy hh:mm
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 2000 total points
ID: 40263442
<?php
$date1=new DateTime("08/01/14 03:21");

$today = new DateTime(date('Y-m-d h:i'));

$diff = $date1->diff($today);

echo (($diff->format('%d')*24) + $diff->format('%h')) . " hours ";
echo $diff->format('%i minutes');

Open in new window

0
 
LVL 1

Author Comment

by:joyacv2
ID: 40263457
Hi Gary,

But how i acquire a result in a format like hh:mm with the hh without the 23 limitation, like 124:45, sometime like [h]:mm in excel?
0
 
LVL 58

Expert Comment

by:Gary
ID: 40263462
Use the code above, I made an edit
0
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 40263466
For that format

echo (($diff->format('%d')*24) + $diff->format('%h')) . ":" . $diff->format('%i');

Open in new window

0
 
LVL 1

Author Comment

by:joyacv2
ID: 40263471
also, this doesn't count in consideration the date, give me only 8 hours
0
 
LVL 1

Author Closing Comment

by:joyacv2
ID: 40263491
works excellent!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question