0

I have date string "Tue, 2 October, 2018" and i need to convert another date format 2018-10-02. but it show 2017 . Why?

   <?php

   $checkin = "Tue, 2 October, 2018";    
   echo date("Y-m-d", strtotime($checkin));

   //it show 2017-10-03 but i need 2018-10-02

   ?>
Min Ko Ko
  • 131
  • 1
  • 2
  • 12
  • You would be better off using the createFromFormat function - see php docs http://php.net/manual/en/datetime.createfromformat.php –  Dec 14 '17 at 06:27

3 Answers3

2

Your format is custom and not parsable by strtotime. You have to make it parsable. Here is how you can do it(DEMO):

$checkin = "Tue, 2 October, 2018";    
$checkinDate = explode(',',$checkin);
$strDate = $checkinDate[1].$checkinDate[2];
echo date("Y-m-d", strtotime($strDate));

This code breaks the string at , and then joins the last 2 parts ignoring the day.

Or you can use date_create_from_format(), to parse this format, like(DEMO):

$checkin = "Tue, 2 October, 2018";
$checkin = date_create_from_format('D, j F, Y', $checkin);
echo date_format($checkin, "Y-m-d");
mega6382
  • 9,211
  • 17
  • 48
  • 69
1

Your input format is wrong. Remove the second comma. It has to read

Tue, 2 October 2018
Kağan Kayal
  • 2,303
  • 1
  • 19
  • 30
1

Remove , comma after October and remove Tue,

Rohit Bhalani
  • 133
  • 1
  • 5
  • 14