Page 1 of 1

Convert Date to Unix

Posted: Fri Jan 11, 2013 6:57 am
by honkmaster
Hi I have the following code I think the date format is wrong.

I'm posting a date from a form as - 2013-01-10, before I post it to mysql I want to convert it to unix format.

The issue I'm having is the unix is incorrect when I convert it bacK

I'm still a beginner so need a bit of help to point me in the right direction

Code: Select all

$wholedate = $_POST['duedate'];
$date_array = explode('.', $wholedate);
$test = mktime(0,0,0,$date_array[1],$date_array[2],$date_array[0]);
echo $test;
echo '<br>';
echo date('Y-m-d', $test); //Check if timestamp returns wanted date format

Re: Convert Date to Unix

Posted: Fri Jan 11, 2013 8:49 am
by twinedev
You have the date separated by hyphens (-) , yet for explode, you are telling to use a period (.)

When you are not getting what you want, start debugging it, echo out values to see if they are what you expect them to be, if you had, you would have seen that $date_array was actually: array(1) { [0]=> string(10) "2013-01-10" }

-Greg

Re: Convert Date to Unix

Posted: Fri Jan 11, 2013 10:26 am
by honkmaster
:D :D :D Greg, thanks for help your advise helped

Code: Select all

$wholedate = $_POST['duedate'];
$date_array = explode('-', $wholedate);
$test = mktime(0,0,0,$date_array[1],$date_array[2],$date_array[0]);
echo $test; //the timestamp I can store in MySQL (BINGO:-)
echo '<br>';
echo date('Y-m-d', $test); //Check if timestamp returns wanted date format