David19801
David19801

Reputation: 11448

PHP Strtotime returns 1

I have the following PHP code:

$te='2011-12-07-14-19-30';
$tt=strtotime($te);
echo $te;
echo $tt;

It's not outputting the ingteger of the time as expected...

Any ideas?

Upvotes: 1

Views: 196

Answers (7)

matino
matino

Reputation: 17725

It returns FALSE, as the date you pass has invalid format.
From docs:

Returns a timestamp on success, FALSE otherwise. Previous to PHP 5.1.0, this function would return -1 on failure.

Upvotes: 0

OptimusCrime
OptimusCrime

Reputation: 14863

$te='2011-12-07-14-19-30';
$arr = explode('-',$te);
$timestamp = mktime($arr[3], $arr[4], $arr[5], $arr[1], $arr[2], $arr[0]);

I think it should work :)

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

$te='2011-12-07 14:19:30'; //y-m-d h:i:s format
$tt=strtotime($te);
echo $te;

Upvotes: 3

Matt Esch
Matt Esch

Reputation: 22966

Try "2011-12-07 14:19:30" or "2011-12-07T14:19:30Z" (to be iso8601 compliant).

Upvotes: 0

Jakub
Jakub

Reputation: 20475

Because this is not a valid time string, it is just numbers separated by dashes. It might be returning -1 instead of 1 as -1 is the default fail response.

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190996

Try using DateTime::createFromFormat.

http://www.php.net/manual/en/datetime.createfromformat.php

Upvotes: 2

Jon
Jon

Reputation: 437654

That is not a valid compound date/time format that strtotime will accept. Modify it appropriately before passing it to the function.

One possible and minimal edit would be to remove all dashes and add a "T" between the date and time part.

Upvotes: 0

Related Questions