Forum Moderators: coopster

Message Too Old, No Replies

date to epoch

converting date into epoch

         

matt1088

6:34 am on Dec 11, 2008 (gmt 0)

10+ Year Member



I have a database that includes a field for the unix timestamp epoch of each row entry.

I'm trying to add a date picker to a new form that submits the date in this format: mm/dd/yyyy. I'm having trouble figuring out how to convert that format to the epoch timestamp.

So far all attempts with using mktime has resulted in me getting todays epoch timestamp added to the database field regardless of what date is entered.

Here's a quick example of the code that produced that incorrect result:

$epochhold = (_$POST['date']);
$epoch = mktime(0,0,0,$epochhold);

Anyone have any ideas?

Thanks in advance!

coopster

2:30 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, matt1088.

unix timestamps and the epoch can give you fits sometimes so be careful and read the warnings on the manual pages for the date/time functions that you use, including mktime [php.net].

The problem you are having here is that (besides the syntax error in your $_POST variable declaration) is that you are not separating the arguments of your

$epochhold
variable into mm, dd, yyyy as the function expects. You are trying to pass the entire string of 'mm/dd/yyyy' as the month argument of mktime.

Alcoholico

4:03 pm on Dec 11, 2008 (gmt 0)

10+ Year Member



You could try strtotime which returns a timestamp.
int strtotime ( string time [, int now] )

$epochhold = ($_POST['date']);
$epoch = strtotime($epochhold);