1

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?

luqita
  • 4,008
  • 13
  • 60
  • 93

4 Answers4

6

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.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

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

RiaD
  • 46,822
  • 11
  • 79
  • 123
0

strtotime works with US dates. Try

$today = date('m/d/y H:i', time());
echo strtotime($today);
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
0

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.

desbest
  • 4,746
  • 11
  • 51
  • 84
  • `strtotime` is a function for turning a string into a timestamp. `date` does the reverse and turns a timestamp into a string. – deceze Aug 02 '11 at 22:52
  • I don't use timestamps ever with php, and insert all dates and times into a mysql database as a VARCHAR string and not has a TIME timestamp. You're right. I didn't know that. For me, `date` is for specifying a date format, and `strtotime` is for changing the format of an outputted date, or moving a date forward or backward in time by a specified time period. – desbest Aug 02 '11 at 23:06
  • If you've ever used `strtotime` or have ever added a number to a time, you have used timestamps. :o) – deceze Aug 02 '11 at 23:15