Jump to content

Recommended Posts

Hi,

 

I am lost. I have a database that has a field in the following format

 

2009-01-28 15:43:52

 

I then have values in php in the format of dd/mm/yyyy. I want to search my db to find the first occurence of a specified date.

 

Can anyone help please?

Link to comment
https://forums.phpfreaks.com/topic/142941-solved-datetime-search-problem/
Share on other sites

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"

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.