Page 1 of 2

Looking for code examples for turning strings into dates

Posted: Mon Jun 05, 2006 4:13 pm
by PastorHank
I'm confused by the use of strtotime(), mktime(), and date(). I have a form that has six pulldowns where I get the year, month and date. (three each for beginning date and ending date).

Where can I find some code examples of how to combine those variables into a format that I can then use in a SQL SELECT statement? I want to be able to find all records greater or equal than my start date and less than or equal to my end date.

I've looked at php.net and searched the forums but I'm just getting more confused.

Thank you

Posted: Mon Jun 05, 2006 4:43 pm
by Chris Corbyn
The best format for strtotime() IMO is Ymd. So today (5th June 2006) would be strtotime('20060605');

It's pretty easy to check you got it right:

Code: Select all

echo date('jS F Y', strtotime('20060605')); //5th June 2006
Combining your dropdowns is just concatenation to make that string.

Code: Select all

$Ymd = $year.$month.$day;

Posted: Mon Jun 05, 2006 4:54 pm
by PastorHank
Thank you, I'll give that a try.

Posted: Mon Jun 05, 2006 4:55 pm
by Burrito
strtotime() can do some wonderful things. If you're using the US standard mm/dd/yyyy it will also convert that to unix time as well.

Posted: Mon Jun 05, 2006 4:57 pm
by PastorHank
Where can I find a really good clear explanation of strtotime()? Especially one with some non-cryptic examples, it seems like it's the one I need to master.

thanks again

Posted: Mon Jun 05, 2006 5:05 pm
by Burrito
the manual is always the best place to check.

Posted: Mon Jun 05, 2006 5:07 pm
by PastorHank
Is that the one at php.net? or is there another one somewhere floating around?

Posted: Mon Jun 05, 2006 5:08 pm
by Chris Corbyn
Burrito wrote:strtotime() can do some wonderful things. If you're using the US standard mm/dd/yyyy it will also convert that to unix time as well.
Indeed. The problem here is that in the UK we use dd/mm/YYYY which looks the same as mm/dd/YYYY so it can get confusing (comment the code!!) :)

Posted: Mon Jun 05, 2006 5:14 pm
by PastorHank
I'm using what used to be called ansi (yyyy/mm/dd) - I'm an old dog trying to learn new tricks....that's why I get confused, there seems to be a plethora of books and sites offering hints and tips and suggestions and they all use initials and abbreviations I've never seen before. I've finally been able to successfully convert all my databases and tables to MySql, but php is giving me more trouble than I'm comfortable admitting....
:?

Posted: Mon Jun 05, 2006 5:19 pm
by Chris Corbyn
If you successfully get the UNIX timestamp from strtotime() you can then use MySQL's FROM_UNIXTIME() to convert that to a MySQL datetime format :)

As for strtotime() I wouldn't get to caught up on it. Just go with one of the formats that's know to work and stick with it :)

Posted: Mon Jun 05, 2006 5:51 pm
by Christopher
PastorHank wrote:Is that the one at php.net? or is there another one somewhere floating around?
Yes. I'd recommend poking around in Language Reference and in the Function Reference for the sections on Arrays, Date/Time, Filesystem, Misc. and String functions. It will probably take you only 15-30 mins and will give you a good feel for the basic stuff in PHP.

Posted: Mon Jun 05, 2006 5:56 pm
by daedalus__
http://www.php.net/download-docs.php

Code: Select all

// Insert
$unix_time = mktime(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year']);
$sql = "INSERT INTO pseudo_table (timestamp)
            VALUES (FROM_UNIXTIME(pseudo_table))";

// Select
$sql = "SELECT UNIX_TIMESTAMP(timestamp)
            FROM pseduo_table";

echo date('d\/m\/Y', $unix_timestamp); // echo's the date in mm/dd/yyyy format
None of the MySQL functions you need are included in that pseduo code but you should get the picture. I hope it helps.

Posted: Mon Jun 05, 2006 7:02 pm
by PastorHank
I think the light is beginning to come on, am i correct in figuring that since the date data I am trying to compare against is stored in a MySql table with the format yyyy-mm-dd (at least according to the MySql query browser), I need to convert my string data from my form into a unix format and I can do that using a Unix_time function....and the unix function should be able to handle the word January and I don't have to do a switch routine to give numeric values to the months?

Posted: Mon Jun 05, 2006 7:07 pm
by RobertGonzalez
How is your form data getting passed? Are you passing strings or numbers?

Posted: Mon Jun 05, 2006 7:13 pm
by PastorHank
Strings - I use three pulldowns, one for months (months are January, etc), one for days one for years (and I'm open to suggestions on a better way to do that also, this seems incredibly cumbersome....)
I have a start date and an end date and I just need to be able to retrieve those records that fall between those values. The data in the database is a Date field.

I'm coming from the Visual Foxpro 9.0 / C## world where I can use a DTOC or CTOD function and life is grand....I haven't worked with Unix based fields in years.....