Convert Date to Unix

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
honkmaster
Forum Newbie
Posts: 12
Joined: Mon Feb 15, 2010 12:10 pm

Convert Date to Unix

Post 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
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Convert Date to Unix

Post 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
honkmaster
Forum Newbie
Posts: 12
Joined: Mon Feb 15, 2010 12:10 pm

Re: Convert Date to Unix

Post 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
Post Reply