John Magnolia
John Magnolia

Reputation: 16793

PHP date formatting basic convert not working

Trying to convert date to mysql format: Y-m-d H:i:s

I know this is real basic and lots of other questions on the same subject but I have been stuck on this for a while now and can't figure out what I have wrong here.

<?php

$user_date = '07/06/2011 06:03';

//$date = date('Y-m-d H:i:s', strtotime(str_replace('/', '-', $user_date)));

$date = DateTime::createFromFormat('d/m/Y H:i', $user_date)->format('Y-m-d H:i:s');

/* $oDate = new DateTime();
$oDate->createFromFormat('d/m/Y H:i', $user_date);
$date = $oDate->format('Y-m-d H:i:s'); */

if($date == '' || $date = '1970-01-01 00:00:00'){
    die('no-'.$date);
}       
die('yes-'.$date);

Upvotes: 0

Views: 45

Answers (2)

MightyPork
MightyPork

Reputation: 18861

Your problem lies herein:

if($date == '' || $date = '1970-01-01 00:00:00'){
    die('no-'.$date);
}  

You used single instead of double equals, hence the second "check" assigned the value, and returned it instead of comparing.

PHP considers non-empty string true, so the condition was met.

Upvotes: 3

rdanusha
rdanusha

Reputation: 923

try this:

  $date = date("Y-m-d H:i:s",strtotime($user_date));

Upvotes: 1

Related Questions