0

I am comparing a date with a datetime and I get the result I expect however I also am wondering if there is a better way to display my output and I have a query on my current output also.

Here is a snippet of my current code:

<?php $todayDate = date('Y-m-d'); $seconds = strtotime($todayDate) - strtotime($dueDate); $hours = $seconds / 60 / 60; echo number_format($hours, 2); ?> 

in my case $dueDate in my database here is 2017-06-26 09:11:28 so the output is displaying as -57.19. My question, is there is a clean way to strip the - and also add h after the hours and m after the minutes so the output looks like this?

57h 19m

UPDATE

So After tinkering around I have managed to do this:

substr($dateFormat,0,3).'h '.substr($dateFormat,4).'m';

The output now is -57h 19m

I still have this negative character, im not sure if that is actually correct I cannot seem to work it out because the date in my database is a day ahead but it shows a negative value...

11
  • Maybe dev.mysql.com/doc/refman/5.7/en/… pull it in minute, multiple by 60. Also note .19 is not 19 minutes, it is 11.4 minutes. .19 * 60. Commented Jun 24, 2017 at 21:14
  • I am not entirely sure what you mean sorry? Commented Jun 24, 2017 at 21:18
  • Send $dueDate with your query then let the DB do the math... or if with the math the decimal part is from a piece of 60, not 100. Commented Jun 24, 2017 at 21:19
  • Your update is going the wrong way, what if there is only a 1 hour difference or a 100+ hour difference? Commented Jun 24, 2017 at 21:23
  • 1
    If you want to convert a negative number to a positive number use absolute function which is built in in PHP like so abs(-10). Yields 10 Commented Jun 25, 2017 at 2:27

3 Answers 3

4

Using the DateTime class makes it very simple

$dueDate = '2017-06-26 09:11:28'; $due = DateTime::createFromFormat('Y-m-d H:i:s', $dueDate); $today = new DateTime(); $diff = $today->diff($due); echo $diff->format('%hh %im'); 

Result:

11h 37m 

But as you asked about timezones, here is how to add those in as well. And also as you orignial date was in fact some days distant I added a more accurate difference output

$dueDate = '2017-06-25 00:00:00'; $due = DateTime::createFromFormat('Y-m-d H:i:s', $dueDate, new DateTimeZone('Europe/London')); $today = new DateTime('now', new DateTimeZone('Europe/London')); $diff = $today->diff($due); echo $diff->format('%R %hh %im').PHP_EOL; if ( $diff->invert ) { echo $diff->format('Overdue by %dd %hh %im'); } else { echo $diff->format('You have %dd %hh %im till overdue'); } 

Results

+ 1h 6m You have 0d 1h 6m till overdue 
Sign up to request clarification or add additional context in comments.

8 Comments

@JaredFarrish Thought I would keep it simple and step by step as I assumed this was the OP's first intro to the DateTime class
@danjbh That because of my local timezone is BST but this is calced in UTC
I changed the variable $dueDate(2017-06-25 00:00:00 in my database column) and the time here is 22:50pm and the output form your code is: 0h 10m
Is there a way it ca understand when the clocks go back and forth?
@danjbh Let me back up: How is the date/timestamp generated? If it's from your server using PHP, use PHP and set the timezone (as RiggFolly shows in the edit). If it's MySQL, you should be able to get the same thing with DATE_FORMAT() and TIMEDIFF() (I believe). The method I was describing was using setTimestamp with a UNIX_TIMESTAMP() from a query.
|
0

You need to keep date integer

$time = time(); 

after

you can use this every where and evert way

For example

$date1 = time(); $date2 = time(); $comparingdate = $date2 - $date1; $myFormat = date("T-m-d h:i:s",$comparingdate); // Show how you want 

2 Comments

So in regards my data above how would this translate? So will time() give me a now timestamp? I Do not see how I can compare a datetime with the time() function?
@danjbh strtotime would convert to the correct format. This is basically what you already have though.
0

use floor and round functions to get the minutes and hours after convert the date to positive sign using abs function

<?php $todayDate = date('Y-m-d'); $dueDate = "2017-06-26 09:11:28"; $seconds =abs(strtotime($todayDate) - strtotime($dueDate)); $hours =floor($seconds / 60 / 60); $minutes= round($seconds / 60 / 60 - $hours,2)*100; echo "<br>"; echo $hours. " H :"; echo $minutes. " M "; ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.