6

I am using datepicker with from and to dates.

When posting these dates in PHP the date format is mm/dd/yyyy.

I need to convert this to MySQL format yyyy-mm-dd

Can it be done like this?

$from = $_GET['from'];
$phpdate = strtotime( $from );
$from_date = date( 'Y-m-d', $phpdate );

I tried this but it doesn't work.

user3312792
  • 1,101
  • 2
  • 12
  • 27

5 Answers5

11

You should use DateTime::createFromFormat

Ex:

$date = DateTime::createFromFormat('m/d/Y','02/10/2015');
echo $date->format("Y-m-d");
// 2015-02-10

So in your case

$from = $_GET['from'];
$date = DateTime::createFromFormat('m/d/Y',$from);
$from_date = $date->format("Y-m-d");
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
2

Try this Check maual here

$from = $_GET['from'];
$phpdate=$from;
$fromdate = date("Y-m-d", strtotime($phpdate)); 
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
1

Try this it will work :

$from = $_GET['from'];
$phpdate=$from;
$fromdate = date("Y-m-d",strtotime($phpdate));
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
0

Try This

$from = $_GET['from'];
$date_array=explode("/",$from);
$new_date_array=array($date_array[2], $date_array[0], $date_array[1]);
echo $new_date=implode("/",$new_date_array);
TECHNOMAN
  • 361
  • 1
  • 9
0
date("Y-m-d", strtotime($_GET['<name>']));
Sagar Patni
  • 312
  • 2
  • 11