1

I am the following code to convert this date -> 18/07/2013 to a mysql supported format.

$date = date('Y-m-d', strtotime('18/07/2013'));

Ideally this should return 2013-07-18...
But, the problem is that it automatically selects the string as:
YEAR as YEAR MONTH as DAY DAY as MONTH

Now, because it identifies 18 as a month, it gets out of range and it shows the default date 1970-01-01.

I have wasted almost two days on this thing.

If anyone can help, thanks in advance!

Safeer
  • 184
  • 3
  • 17
  • 2
    You could hack it as `strtotime(str_replace('/','-','18/07/2013'))` – Andy Gee Jul 02 '13 at 07:51
  • Kindly use the search before posting a new question, this is a duplicate of [Strtotime() doesn't work with dd/mm/YYYY format](http://stackoverflow.com/questions/2891937/strtotime-doesnt-work-with-dd-mm-yyyy-format) – Prix Jul 02 '13 at 07:52
  • From the php manual -> YY "/" MM "/" DD this is what you are supposed to use with the date function... http://php.net/manual/de/datetime.formats.date.php – SQL.injection Jul 02 '13 at 07:53
  • @Prix I'll keep that in mind the next time :) – Safeer Jul 02 '13 at 07:59

6 Answers6

5

Use DateTime object, to get php understand in which format you passing date to it.

$date = DateTime::createFromFormat('d/m/Y', '18/07/2013');
echo $date ->format('Y-m-d');
Rikesh
  • 26,156
  • 14
  • 79
  • 87
4

Try this :)

echo date('Y-m-d', strtotime(str_replace('/', '-', '18/07/2013')));
Ayyappan Sekar
  • 11,007
  • 2
  • 18
  • 22
1

strtotime parameter should be in format 18-07-2013

Rohitashv Singhal
  • 4,517
  • 13
  • 57
  • 105
1

You need to set the time zone of your date, your date is being parsed as being American and the American date format is mm/dd/yyyy...

date_default_timezone_set('Europe/Berlin');

also use the right reg. expression for the date function

$date = date( 'YY "/" MM "/" DD', strtotime('18/07/2013'));
SQL.injection
  • 2,607
  • 5
  • 20
  • 37
0

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

Observe:

echo date("jS F, Y", strtotime("11.12.10")); 
// outputs 10th December, 2011 

echo date("jS F, Y", strtotime("11/12/10")); 
// outputs 12th November, 2010 

echo date("jS F, Y", strtotime("11-12-10")); 
// outputs 11th December, 2010  
Alejandro Colorado
  • 6,034
  • 2
  • 28
  • 39
0

The problem is with date string in PHP

  • Date values separated by slash are assumed to be in American order: m/d/y
  • Date values separated by dash are assumed to be in European order: d-m-y

Everything you wan to know about conversing date is below

http://shakalya.com/resolve-php-date-format-problem/

  • whats wrong here... ideally if you use 18/07/2013, first number is considered as MONTH But if you use 18-07-2013 then first number is a DAY. technically writing 18/07/2013 is wrong to write in PHP code because 18 cant be a month. SO change your naming convention according to codes. TO work with it change / with - then convert it to date. – Shakalya Uttam Mar 08 '19 at 04:58