Php date shows 1970 instead of inserted date

hi all, i got this default date mysql format. which i wanna call like mm-dd-yyyy. i tried date function but its returning 01/01/1970. here is my code

date("m/d/Y",$this->mevents[$i]->date)

what i m doing wrong?

The default MySQL date format is yyyy-mm-dd or unix timestamp. I guess $this->mevents[$i]->date is not an integer (unix timestamp), so the date function defaults to 0 which is 1970-01-01.

MySQL’s DATE field is Y-m-d. It’s DATETIME field is Y-m-d H:i:s.
If you’re trying to take output from a mysql date field and translate it into your format, strtotime the result first, because date()'s second parameter is a timestamp, not a datestring.

date(“m/d/Y”,strtotime($this->mevents[$i]->date))

Thanks Starlion it helped, i think it was string like adam said. though i am using calender on front end. it has format of yyyy-mm-dd, if i swap the mm-dd-yyyy. the values in DB goes as 00-00-0000. why is that?

MySQL’s date field can only take in strings in the format Y-m-d.

If you want to store a date in mysql, you will have to reformat it into that format.

Example:
$_POST[‘date’] is a field coming in from your calendar in m-d-y form.


$date = date('Y-m-d',strtotime($_POST['date']))
$query = "INSERT INTO table(`date`) VALUES('".$date."')";

(Note: This will also sanitize the date, as strtotime will return -1 if the post value is not a time-string.)