PHP- Hiding unused time stamp references -
i have come across issue not sure how approach. comparing time time in future difference in time or in case, time remaining , part works great. here snippet reference issue.
$current = date('m/d/y h:i:s a', time()); $expired = date('m/d/y h:i:s a', strtotime($expires)); $dtestart = new datetime($current); $dteend = new datetime($expired); $dtediff = $dtestart->diff($dteend); then in html insert following...
echo $dtediff->format("%ad %hh %im"); as stated works great 1 exception. produces unused time references. example.
remaining: 0d 0h 30m
as can see don't need day or hour show because not used. stuck since 1 single variable display time not sure how hide unused characters not needed when showing 0.
show how can hide ones show 0 , if 3 show 0 show word expired? wrapped in isset() though there multiple values in 1 single variable? suggestions?
-thanks
try this:
$i = $dtediff->format(" %ad %hh %im"); $i = str_replace(" 0d","",$i); if (strpos($i, 'd') == false) { $i = str_replace(" 0h","",$i); if (strpos($i, 'h') == false) { $i = str_replace(" 0m","",$i); } } $i = trim($i); if ($i == "") { $i = "expired"; } echo $i;
Comments
Post a Comment