Welcome Guest, Not a member yet? Register   Sign In
Code needed to convert human date input from web form to UNIX timestamp
#1

[eluser]edziffel[/eluser]
Needed is a solution to take human date input from a web page form and convert it to a UNIX timestamp.

The application stores all date/time data in the database as a UNIX timestamp using the PHP function time() with the intent of making the code more portable for differing databases-read engineering Oracle out of the equation. The human input will be only mm/dd/yyyy (no hours min etc.) or some iteration of that depending on further reasearch, but still must be stored in the db as a Unix timestamp. The human inputted timestamps need to be searchable in day increments and human backdating and postdating will be allowed.

Questions:
1. Can/how does one use the human_to_unix() function for such a requirement?
2. Are there other approaches that would work better?

Thanks,

Ed
#2

[eluser]bretticus[/eluser]
[quote author="edziffel" date="1245562247"]
Questions:
1. Can/how does one use the human_to_unix() function for such a requirement?
[/quote]
I haven't looked under the hood on this one, but this CI function is probably just a wrapper for strtotime(). Look there for examples or read up on loading helpers for CI. human_to_unix() is part of the date helper. Here's a small example:

Code:
function someaction()
    {
        $this->load->helper('date');
        echo human_to_unix($this->input->post('human_date'));
    }
[quote author="edziffel" date="1245562247"]
2. Are there other approaches that would work better?
[/quote]

For converting human dates to unix dates in PHP?
#3

[eluser]edziffel[/eluser]
For converting human dates to unix dates in PHP?

Yes. Need to convert dates from an input form to Unix timestamps. That will avoid issues working with dates generating views.

Have checked out the CI manual date helper page as well as searching the forums. It doesn't give the specifics on how to format the human side to be ready to convert. It states that it needs be in the above format YYYY-MM-DD HH:MM:SS AM/PM, the format for UNIX to human, but doesn't have an example of the required input for Human to UNIX ie does one need to concatenate the user input to exactly match that input format and are the 2nd and 3rd parameters HH:MM:SS, AM/PM optional minus the switches?

Maybe this is a candidate for the “guess change and repeat” method to see what works. How long could it take?
#4

[eluser]edziffel[/eluser]
Already had the helper loaded in my controler

Dug around on the php site and looks like, at the moment anyway, that the strtotime function might work out. The unix_to_human works great. Thanks for the direction. If some knows a better way...

Ed
#5

[eluser]bretticus[/eluser]
Just looked "under the hood" and I can see that human_to_unix() is definately not a wrapper for strtotime(). This function is made to go in the opposite direction of unix_to_human(). strtotime() may be more versatile being a native php function with much variation detection built in.

I'd suggest giving the user a calendar control of some kind though since that is easy for the user and enforces a date/time format. Codeigniter has a built in library for doing this server-side. jquery has a nice plugin for doing this client side. Both seem fairly easy to implement.




Theme © iAndrew 2016 - Forum software by © MyBB