/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] datetime in version 5.5.37

Having a problem getting the elapsed time between two dates. Following examples I have created two datetime objects, verified with a print_r but when I attempt to use date_diff on them I get an error in that the formatted output is simply just that: the format string.

My code:

[code=php]
$last_dose = date_create_from_format(‘Y-m-d h:i a’,”$datetaken 08:15 am”);
$curr_time = new datetime();
echo “last dose: “,print_r($last_dose,true).”<br>”;
echo “curr time: “,print_r($curr_time,true).”<br>”;

$interval = date_diff($last_dose, $curr_time);
echo “elapsed is: “.$interval->format(‘d h:l’);
[/code]

The output from the two print_r shows exactly what I expect to see, and they show as datetime objects as well. The interval output is just the format string itseld: d h:l.

According to the manual apparently the date_diff and the format functions are only good for :

(PHP 5 >= 5.3.0, PHP 7)

So what do I have to use with my current install?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 19.2017 — I got it to work after adding the "%" characters for the format string (apparently it's like sprintf() instead of like date()). Also, that's a capital I for the seconds, not a lower-case l.

[code=php]
<?php
$datetaken = '2017-02-11';
$last_dose = date_create_from_format('Y-m-d h:i a',"$datetaken 08:15 am");
$curr_time = new datetime();
echo "last dose: ",print_r($last_dose,true)."n";
echo "curr time: ",print_r($curr_time,true)."n";

$interval = date_diff($last_dose, $curr_time);
var_export($interval);
echo "n";
echo "elapsed is: ".$interval->format('%d %h:%I')."n";
[/code]
Copy linkTweet thisAlerts:
@ginerjmauthorFeb 19.2017 — Thanks for the info. Seems that the manual left something out.

Thx for the quick hand.
Copy linkTweet thisAlerts:
@ginerjmauthorFeb 19.2017 — Upon further review - the manual does show the % but the samples I googled did not. Oh, well..
×

Success!

Help @ginerjm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...