-2

I have code:

$date= "2016/Apr/16";
$time="5:00 PM";

I want to convert it to DB format like 2016-04-16 17:00:00 in php . I am not getting exact code from anywhere.

Tomasz
  • 4,847
  • 2
  • 32
  • 41
unknownbits
  • 2,855
  • 10
  • 41
  • 81

2 Answers2

1

This way will solve your problem.

<?php
$date= "2016/Apr/16";
$time="5:00 PM";
$fullDate = str_replace('/','-',$date) . " " . $time;
$newDate = date("Y-m-d H:i:s", strtotime($fullDate));
echo $newDate;
?>

Result

More Examples;

http://www.w3schools.com/php/func_date_strtotime.asp

Convert date format yyyy-mm-dd => dd-mm-yyyy

Community
  • 1
  • 1
B.Kocaman
  • 800
  • 4
  • 13
-2

Normally you would use the following code:

date('Y-m-d H:i:s', strtotime($date.' '.$time));

If it returns the year 1970, then the input date (the one from $date variable) is not formatted according to a standard that can be recognised by php.

Ibrahim
  • 2,034
  • 15
  • 22