Jump to content

[SOLVED] DateTime search problem


cleary1981

Recommended Posts

What do you mean by 'first occurence'. The one closest to 2009-01-28 00:00:00 ?

 

Anyway

 

$date = "28/01/2009";
$dateArr = explode("/",$date); //split the date into array using / as delimiter
$dateMySql = "{$dateArr[2]}-{$dateArr[1]}-{$dateArr[0]}";  //create astring with date formatted in MySQL compatible format
$query = "SELECT * FROM table WHERE DATE(dateTimeField) = '$dateMySql' ORDER BY orderField ASC LIMIT 1";

 

orderField is the field by which you sort data in your table, to get 'first occurence' of date. If, as I said, you want date closest 2009-01-28 00:00:00, then instead of 'orderField' you should put the name of your dateTimeField.

 

I'm having a similar problem. I need to retrieve data with yesterday's date.

 

If I generate today's date using date("Y-m-d"); it shows up in my database with leading zeroes (2009-01-29). Can I explode that at the "-" and then manipulate the arrays as numbers or will they be strings?

Why not just generate yesterday's date?

 

echo date("Y-m-d",strtotime("yesterday"));

 

*rubs his eyes* I must not be reading that right. Wouldn't that generate todays date and save it in the variable "yesterday"?

I'm not familiar with the code "strtotime"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.