0

When i run the following code

echo date('l jS \of F Y', strtotime('22/08/2018'));

the output is

Thursday 1st of January 1970

Any ideas why?

Qirel
  • 23,315
  • 7
  • 41
  • 57
Mensur
  • 419
  • 8
  • 26

2 Answers2

0

It is to do with the difference between US an European date formats.

With slashes strtotime reads MM/DD/YY with hyphens it reads DD-MM-YY

Replacing the slashes with dashes will tell it to use the correct format.

echo date('l jS \of F Y', strtotime('22-08-2018'));

Will print Wednesday 22nd of August 2018

...Forward slash (/) signifies American M/D/Y formatting, a dash (-) signifies European D-M-Y and a period (.) signifies ISO Y.M.D.

From the most up voted User Contributed Notes on: https://secure.php.net/manual/en/function.strtotime.php

Andreas
  • 23,304
  • 5
  • 28
  • 61
OrderAndChaos
  • 3,260
  • 1
  • 26
  • 51
0

The reason you get that output is because strtotime can't parse the date.
If you look in the manual it has a list of what date formats that can be parsed.

If you must use that date format you can use date_create_from_format to parse it.

$datetime = date_create_from_format ( "d/m/Y" , $dateString);

This returns a DateTime object that you can manipulate as you wish.
Example:

$dateString = '22/08/2018';
$datetime = date_create_from_format ( "d/m/Y" , $dateString);

echo date_format ( $datetime , 'l jS \of F Y' );

Will output:

Wednesday 22nd of August 2018

https://3v4l.org/WLgNK

Andreas
  • 23,304
  • 5
  • 28
  • 61