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

Post by PastorHank »

Ok, this is where I have gotten

Code: Select all

$startingdate1=($yeartostart.$monthnumberone.$daytostart);
$startingdate=strtotime($startingdate1);

/* echo date('Y/j/n g:i:sa', $startingdate); */
echo date('Y/n/j', $startingdate);
returns 2005/7/15 which is the correct date and it is a date that I know is in the file - now the final question is how do I assign the value that comes from the date() function to a variable

I've tried this

Code: Select all

$datetouse = date('Y/n/j', $startingdate)
   $query = "SELECT * FROM thunde9_thunderhill.fawns.dateofbirth= $datetouse ORDER BY 7 DESC";
   $result = mysql_query($query)
		or die ("Couldn't Execute Query.");
It returns the message - 'Couldn't execute Query -- I'm getting closer

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: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.
If you are getting the dates from year/month/day selects then you can probably just do:

Code: Select all

$yeartostart = (int) $_POST['yeartostart'];
$monthtostart = (int) $_POST['monthtostart'];
$daytostart = (int) $_POST['daytostart'];
$startingdate="$yeartostart-$monthtostart-$daytostart";

$yeartoend = (int) $_POST['yeartoend'];
$monthtoend = (int) $_POST['monthtoend'];
$daytoend = (int) $_POST['daytoend'];
$endingdate="$yeartoend-$monthtoend-$daytoend";

$sql = "SELECT * FROM table WHERE (dateofbirth >= '$startingdate') AND (dateofbirth <= '$endingdate') ORDER BY dateofbirth DESC";
(#10850)
PastorHank
Forum Contributor
Posts: 117
Joined: Sat Jun 03, 2006 7:58 am
Location: Texas Hill Country

Post by PastorHank »

Christopher,
That worked, thank you
Post Reply