Ramkumar P
Ramkumar P

Reputation: 206

Date function in PHP Not working properly

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

Upvotes: 0

Views: 4878

Answers (3)

Mayank Vadiya
Mayank Vadiya

Reputation: 1451

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

Upvotes: 1

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

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

Upvotes: 3

Pieter21
Pieter21

Reputation: 1845

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)

Upvotes: 1

Related Questions