luqita
luqita

Reputation: 4077

PHP: Problem with strtotime

What's going on with strtotime here?

$today = date('m.d.y H:i', time());
echo strtotime($today);

It does not output anything... What's going on?

Upvotes: 1

Views: 3994

Answers (4)

desbest
desbest

Reputation: 4896

strtotime() is a function for formatting the date, before it is outputted. It seems like the date is already formated in the date() function, and that you make no attempt to format the date in the second line.

Correct code

$today = date("Y-m-d-H.i");
$datenumber = date('Y-m-d',strtotime($today));
$timenumber = date('H.i',strtotime($today));

You can echo all those variables.

Upvotes: 0

Ray Toal
Ray Toal

Reputation: 88378

strtotime works with US dates. Try

$today = date('m/d/y H:i', time());
echo strtotime($today);

Upvotes: 0

deceze
deceze

Reputation: 521995

strtotime can only parse certain formats, not any random assortment of numbers and letters. "m.d.y H:i" is not a format strtotime can parse. You'll need to parse that manually using, for example, strptime.

Upvotes: 6

RiaD
RiaD

Reputation: 47619

Use DateTime::createFromFormat() if you know source format of date ('m.d.y H:i') in your example

print DateTime::createFromFormat('m.d.y H:i',$date)->getTimestamp()

Manual
DateTime::createFromFormat
DateTime::getTimestamp

Upvotes: 0

Related Questions