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
  • 25,449
  • 7
  • 45
  • 62
Mensur
  • 457
  • 9
  • 28

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,610
  • 6
  • 30
  • 62
OrderAndChaos
  • 3,547
  • 2
  • 30
  • 57
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,610
  • 6
  • 30
  • 62