0

That get i use code:

$then = new DateTime(date('Y-m-d H:i:s', $seconds_end));
$now = new DateTime(date('Y-m-d H:i:s', time()));
$diff = $then->diff($now);

But in var_dump($diff); i see:

object(DateInterval)#4 (8) { 
                           ["y"]=> int(0) 
                           ["m"]=> int(0) 
                           ["d"]=> int(6) 
                           ["h"]=> int(2) 
                           ["i"]=> int(1) 
                           ["s"]=> int(17) 
                           ["invert"]=> int(1)
                           ["days"]=> int(6) 
                           }

Tell me please how get y,m,d,h,i,s with zeros, that ,ex., $diff['h'] will be '02' instead of '2' ?

3 Answers3

2

Just use "format" method of DateInterval: http://php.net/manual/ru/dateinterval.format.php

eg:

$diff->format('%Y-%M-%D %H:%I:%S');

If you only want to get one of attributes, use sprintf or str_pad:

sprintf('%02d', $diff->d);

str_pad($$diff->d, 2, '0', STR_PAD_LEFT);
Bohdan Yurov
  • 365
  • 5
  • 9
  • **not worked** `$diff->format('Y-m-d H:i:s'); object(DateInterval)#4 (8) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(6) ["h"]=> int(1) ["i"]=> int(42) ["s"]=> int(4) ["invert"]=> int(1) ["days"]=> int(6) }` –  Dec 08 '13 at 10:16
  • call var_dump on method result: var_dump($diff->format('Y-m-d H:i:s')), not the object itself. It certainly works. – Bohdan Yurov Dec 08 '13 at 10:17
  • `string(11) "Y-m-d H:i:s"` –  Dec 08 '13 at 10:18
  • I'm sorry, "%Y-%M-%D %H:%I:%S" – Bohdan Yurov Dec 08 '13 at 10:21
  • **sorry but i make** `$diff->format('%Y-%M-%D %H:%I:%S'); var_dump($diff);` **i again get result** `object(DateInterval)#4 (8) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(6) ["h"]=> int(1) ["i"]=> int(28) ["s"]=> int(39) ["invert"]=> int(1) ["days"]=> int(6) }`... –  Dec 08 '13 at 10:31
  • but when i use `var_dump($diff->format('%Y-%M-%D %H:%I:%S'))` i get `string(17) "00-00-06 01:25:50"`... what problem can been? –  Dec 08 '13 at 10:32
  • And again, use $diff = $diff->format('%Y-%M-%D %H:%I:%S'); var_dump($diff); You are calling "format" method, and var_dump'ing the object, instead of result. Also I updated answer, check another way to do this. – Bohdan Yurov Dec 08 '13 at 10:32
  • -- what problem can been? What do you want to achieve? If you need formatted string - this is the answer. If you need only one of parameters, use sprintf. – Bohdan Yurov Dec 08 '13 at 10:33
  • yes, im sorry, i not use sprintf('%02d', $diff->m) in result. all right, thanks. –  Dec 08 '13 at 10:40
0

you can check the string length if its not 2 then add a 0(zero) string, like follows

if(strlen($diff->h)==1) 
{
$new_hr='0'.$interval->h;   
}
echo $new_hr;
0

You are doing a few things wrong here. You don't need to use date() with DateTime objects and DateTime::createFromFormat() is more appropriate for your use case. Your example code should look like this:-

$then = DateTime::createFromFormat('Y-m-d H:i:s', $seconds_end);
$now = new DateTime(); // Defaults to current date & time
$diff = $then->diff($now);

Then you can output in any format you wish:-

echo $diff->format("Time difference = %Y Years %M Months %D Days %H Hours %I Minutes %S Seconds");    

Which will produce something like:-

Time difference = 00 Years 00 Months 06 Days 23 Hours 17 Minutes 38 Seconds
vascowhite
  • 18,120
  • 9
  • 61
  • 77