Looking for code examples for turning strings into dates
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
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
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
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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:
Combining your dropdowns is just concatenation to make that string.
It's pretty easy to check you got it right:
Code: Select all
echo date('jS F Y', strtotime('20060605')); //5th June 2006Code: Select all
$Ymd = $year.$month.$day;-
PastorHank
- Forum Contributor
- Posts: 117
- Joined: Sat Jun 03, 2006 7:58 am
- Location: Texas Hill Country
-
PastorHank
- Forum Contributor
- Posts: 117
- Joined: Sat Jun 03, 2006 7:58 am
- Location: Texas Hill Country
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
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
-
PastorHank
- Forum Contributor
- Posts: 117
- Joined: Sat Jun 03, 2006 7:58 am
- Location: Texas Hill Country
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....

- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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.PastorHank wrote:Is that the one at php.net? or is there another one somewhere floating around?
(#10850)
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
http://www.php.net/download-docs.php
None of the MySQL functions you need are included in that pseduo code but you should get the picture. I hope it helps.
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-
PastorHank
- Forum Contributor
- Posts: 117
- Joined: Sat Jun 03, 2006 7:58 am
- Location: Texas Hill Country
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?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
PastorHank
- Forum Contributor
- Posts: 117
- Joined: Sat Jun 03, 2006 7:58 am
- Location: Texas Hill Country
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.....
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.....