Date diff customized output in php -


here php code. working fine.

<?php ........     $cdt1 = date("y-m-d h:i:s");                  $last_seen = "2015-05-20 12:15:20";      $datetime22 = new datetime($cdt1);      $datetime11 = new datetime($last_seen);      $interval1 = $datetime11->diff($datetime22);  echo $interval1->format('%y years %m months , %d days %h hours, %i min , %s sec '); ......... ?> 

it giving me output this. 0 years 0 months 0 days 00 hours 1 min , 52 sec. that.

i want

  if year 0 year doesn't show.   if month 0 month doesn't show.   if days 0 days doesn't show.   same hour , min well. 

e.g if difference of time 1 hour 24 min 30 sec should appear 1 hour 24 min 30 sec. don't want year/month/days if 0.

please advise.

one option using regular expression:

$date = $interval1->format('%y years %m months , %d days %h hours, %i min , %s sec ');  $replaced = false; while ($replaced) {     $date = preg_replace('/^(0 [a-z]+)/', '', ltrim($date, " ,and"), 1, $replaced); } 

notice ^ character showing beginning of regular expression, fourth parameter make 1 replacement @ time , fith 1 use bool (integer) flag in while.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -