0

While converting date format from mm-dd-yy hh:ii:ss to yy-mm-dd hh:ii:ss format using below code.

<?php
echo $start_date =  date("Y-m-d H:i:s",strtotime("10-14-2015 00:00:00"));
?>

But the result is

1970-01-01 05:30:00

If it is not a proper way to use date ,provide me alternate way

Ramkumar P
  • 196
  • 3
  • 13

3 Answers3

3

First check this answer whats the difference between dates over here and simply use str_replace like as

echo $start_date =  date("Y-m-d H:i:s",strtotime(str_replace("-","/","10-14-2015 00:00:00")));

Or you can also use DateTime::createFromFormat over like as

$date = DateTime::createFromFormat("m-d-Y H:i:s","10-14-2015 00:00:00");
echo $date->format('Y-m-d');

Demo

Community
  • 1
  • 1
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
1

Read the manual: http://php.net/manual/en/function.strtotime.php and also specify your PHP version.

It has a nice example on checking if strtotime conversion was successfull:

if (($timestamp = strtotime($str)) === false) {

And this conversion is very critical, it supports only limited number of formats that should be specified very precisely in order days/months/years, and separators / or - or :

So you have to pick the format that you will support from following list: http://php.net/manual/en/class.datetime.php

Be sure to create culturally aware code (e.g. US/UK/... format)

Pieter21
  • 1,765
  • 1
  • 10
  • 22
1

Please write your code as below: echo $start_date = date("Y-m-d H:i:s",strtotime("10/14/2015 00:00:00"));

Mayank Vadiya
  • 1,437
  • 2
  • 19
  • 32