qadenza
qadenza

Reputation: 9293

Creating time from php variables

Echoing variables I'm getting correct values, but creating a date - something is wrong.

echo $d;  // 0
echo $e;  // 0

$date = date("H-i", strtotime($d."-".$e));

echo $date; // 1-0

I expected 0-0

Any help

Upvotes: 0

Views: 25

Answers (1)

Matthew Usurp
Matthew Usurp

Reputation: 783

The string 0-0 is not actually valid, you must supply either a valid date or time formatted string. Some examples being 2018-01-26 and 10:10:10. There are some shortcuts you can take, maybe checkout the manual for this information (links below). Currently you will find that false is being returned from strtotime('0-0') and when given to date() will probably default to the current time.

Fix by replacing - in your argument for strtotime() with the colon :.

php > echo date('H-i', strtotime('0:0'));
00-00

Not exactly your expectation, but since H and i will always return a string that is of length 2 its as close as you can get. The manual states "with leading zeros", links provided again.

Upvotes: 2

Related Questions