Jump to content

Convert Date to unix


honkmaster

Recommended Posts

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 I'm still a beginner so need a help Cheers Chris

$wholedate = $_POST['duedate'];$date_array = explode('.', $wholedate);$test = mktime(0,0,0,$date_array[1],$date_array[0],$date_array[2]);

Link to comment
Share on other sites

what's the problem? what is the value of $test? from the documentationhttp://php.net/manual/en/function.mktime.php the params are hour, minute, second, month, day, year, so I think your order is wrong. i.e.

echo $date_array[0]; //should echo 2013, the yearecho $date_array[1]; //should echo 01, the monthecho $date_array[2]; //should echo 10, the day

so I think you would want to do something like this

$test = mktime(0,0,0,$date_array[1],$date_array[2],$date_array[0]);

this is a good site for testing things like this for accuracyhttp://www.epochconverter.com/

Link to comment
Share on other sites

Do you mean to explode that string with a dot delimiter and not a hyphen delimiter? Anyway-- 2013-01-10 (with hyphens, not dots) is already a validly formatted date string* to pass directly to strtotime. The result is a Unix timestamp. EG: echo strtotime('2013-01-10'); // 1357804800 * as long as that is year-month-day format

Link to comment
Share on other sites

Thanks for response and sorry for not stating the issue, when I run the code and check the unix date I get 1354233600 = 2012.11.30 ?? and not 2013-01-10 as expected. Cheers Chris
that doesn't make sense. your post is specifically asking to get a UNIX timestamp, which it appears you are getting. so what is the issue then?
Link to comment
Share on other sites

This is code i'm using now, only the returned unix date is incorrect?

$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

Link to comment
Share on other sites

When I run that code with 2013.01.10 , it works as expected. When I run it with 2013-01-10 , it returns 2012-11-30, which you say is the value you have been getting also. In my first post I asked "Do you mean to explode that string with a dot delimiter and not a hyphen delimiter?" Clearly this has been your problem from the start. The delimiters don't match. What's happening is this: because explode cannot find a "." character, an array with one element is being created. 2013-01-10 does not match the interpreter's expectations of of an integer, so it is casting the value to 2013 by itself. The undefined "values" are being interpreted as 0. Since mktime interprets numbers the way humans do, the first month is 1 and the first day is 1. Numbers less than one go backwards. So the first 0 puts you backward into December of 2012. The second 0 takes you back one day to the last day of November, 30. You would get some hints about this if you turned on error reporting. This is what I see. Line 5 is the mktime line. Notice: Undefined offset: 1 on line 5Notice: Undefined offset: 2 on line 5Notice: A non well formed numeric value encountered on line 5 I hope some of that makes sense.

Edited by Deirdre's Dad
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...