Welcome Guest, Not a member yet? Register   Sign In
Date Formatting Help: %m/%d/%y to unix
#1

[eluser]JaredKC[/eluser]
I have a form that lets the user enter the date formatted as Month/Day/Year (%m/%d/%y). I have been able pull the unix date from the database and format as needed, but not the other way around.

Here is the code I am using to format the unix date as %m/%d/%y:
Code:
$datestring = "%m/%d/%y";
                $thisdate = $project->date;
                $project_date = mdate($datestring, $thisdate);

Anyone know how I can take %m/%d/%y and change it to unix time?

Thanks
#2

[eluser]Mike Ryan[/eluser]
Hi Jared,

You want the PHP mktime function.

Split your date so you have $month, $day, $year, then do:

Code:
$timestamp = mktime(0, 0, 0, $month, $day, $year)
#3

[eluser]slowgary[/eluser]
PHP has a wonderful function called strtotime() (string to time). It shouldn't have a problem converting your string to a timestamp. It can even do things like strtotime('+2 weeks -1 day'), and return a timestamp of 13 days from now.
#4

[eluser]JaredKC[/eluser]
Mike Ryan's reply gave me an idea using the CI date helper. Here is my solution.

Code:
$date = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day']. " 01:01:01 PM";
$unix = human_to_unix($date);

I am just creating a date string that matches the CI human readable date format then using the human_to_unix function. I should of course insert the current time but for testing I just manually inserted a time.
#5

[eluser]Mike Ryan[/eluser]
Glad my suggestion could help. btw, the human_to_unix function is basically a wrapper for mktime - it will be more efficient to do:
Code:
$unix = mktime(0,0,0,$_POST['day'],$_POST['month'],$_POST['year']);

if you are sure that your $_POST values are valid. The result will be the same, but you will save yourself the cost of some variable assignments and regex matches.




Theme © iAndrew 2016 - Forum software by © MyBB