Looking for code examples for turning strings into dates

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

PastorHank
Forum Contributor
Posts: 117
Joined: Sat Jun 03, 2006 7:58 am
Location: Texas Hill Country

Looking for code examples for turning strings into dates

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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;
PastorHank
Forum Contributor
Posts: 117
Joined: Sat Jun 03, 2006 7:58 am
Location: Texas Hill Country

Post by PastorHank »

Thank you, I'll give that a try.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
PastorHank
Forum Contributor
Posts: 117
Joined: Sat Jun 03, 2006 7:58 am
Location: Texas Hill Country

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

the manual is always the best place to check.
PastorHank
Forum Contributor
Posts: 117
Joined: Sat Jun 03, 2006 7:58 am
Location: Texas Hill Country

Post by PastorHank »

Is that the one at php.net? or is there another one somewhere floating around?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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!!) :)
PastorHank
Forum Contributor
Posts: 117
Joined: Sat Jun 03, 2006 7:58 am
Location: Texas Hill Country

Post 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....
:?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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.
PastorHank
Forum Contributor
Posts: 117
Joined: Sat Jun 03, 2006 7:58 am
Location: Texas Hill Country

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

How is your form data getting passed? Are you passing strings or numbers?
PastorHank
Forum Contributor
Posts: 117
Joined: Sat Jun 03, 2006 7:58 am
Location: Texas Hill Country

Post 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.....
Post Reply