I want to allow my users to search the database for a row that was submitted on a certain day. The date is entered into the field in the database using the date() function which is great, but when i use the php strtotime function, of course the dates are not exactly the same.
Is there some clever mysql function that can help me with this?
I had an issue with this before.
You're best to generate a start and end date then use the BETWEEN function instead.
So something like this:
$start_date = "2009-01-01 00:00:00";
$end_date = "2009-01-01 23:59:59";
$sql = "SELECT * FROM table WHERE date BETWEEN '$start_date' AND '$end_date' AND id = 'x';
Of course you would just pull your dates from the DB using strtotime and append the time stamps - depends how you used date()
Hope this helps :)
You can use MySQL's DATE() function to extract date part of the timestamp:
select ... from table ... where DATE(date_column) = '2010-01-25';
If you have problem submitting '2010-01-25' from PHP, you can use PHP's date function with 'Y-m-d' as parameter to only get the date part.
$d = date('Y-m-d', strtotime(...));
Looking at your question closely, it seems you'll need both of those. First use PHP's date function to get only the date part and then use MySQL's date to match only those records.
PHP:
$timestamp_from_php = strtotime('December 25, 2009');
SQL:
select
`fields`
from
Table t
where
date_format('Y-m-d', t.`datetime_field`) = date_format('Y-m-d', '$timestamp_from_php')
Related
My query below doesn't seem to work.
SELECT * FROM `test` WHERE date between '12/30/2013' and '01/05/2014'
But when I change the order of dates like in this query below, it seems to be working
SELECT * FROM `test` WHERE date between '01/01/2014' and '01/05/2014'
What is the correct way to use date ranges in SELECTs?
It won't work because the dates are not ISO 8601 formatted: "2013-12-30" for example.
The BETWEEN clause makes a string comparaison, so you need either to use a correct date format in your database, or format the dates with DATE_FORMAT() or STR_TO_DATE(str,format).
Edit:
Try this query, which will work if you store dates as strings formatted as %m/%d/%Y, which is a bad idead, MySQL has a built-in DATE format :
SELECT * FROM test where STR_TO_DATE(date, '%m/%d/%Y') between STR_TO_DATE('01/01/2014', '%m/%d/%Y') and STR_TO_DATE('01/05/2014','%m/%d/%Y');
The DATE_FORMAT MySQL takes a DATE or DATETIME value as first argument, which you don't use, that's why it didn't work (in addition to the '$' you used instead of '%' before 'Y')
Use php and sql together to get the result as below
$date1 = date("Y-m-d", strtotime('12/30/2013'));
$date2 = date("Y-m-d", strtotime('01/05/2014'));
$sql = "SELECT * FROM `test` WHERE date between '".$date1."'
and '".$date2."'";
select * from test where date between "2013-12-30" and "2014-01-05"
This will work fine
How would i go about checking to see if an auction has expired in my database? I have a datetime column in MySQL that i believe is of the following format: YYY-MM-DD hh:mm:ss. If this is the case would the following check work - i.e. want to select only expired auctions from the table in the database...
<?php
//Some code
$auctioncheck = mysql_query("
SELECT * FROM auction WHERE ($date_time > finish_time)
");
?>
While "finish time" is a column in the database of the above cited format. Presuming this works how actually do i get the current date into the same format? If anybody knows i would very grateful cheers. Even more so if the above query wouldn't work and something else is required. Thanks again.
Oh and of course i would define the date_time variable to start with
Do you actually need the $date_time variable? The easiest way to do this would be SELECT * FROM auction WHERE finish_time < NOW(). That way you'll get your results and don't have to set the date from PHP.
You can use
<?php
//Some code
$date_time=strtotime($date_time);
$auctioncheck = mysql_query("
SELECT * FROM auction WHERE ( $date_time > UNIX_TIMESTAMP(finish_time))
");
?>
This solution:
// to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');
// or, to show date only
$date->get('YYYY-MM-dd')
is from this post: PHP Zend date format
and here's another solution: how to format a Date in MM/dd/yyyy HH:mm:ss format in javascript?
and to do it in php:
How to get the current date and time in PHP?
The function date is made for it:
// Get the current date
$date_time = date("y-m-d H:i:s");
Look at the documentation page to see other format flags ;)
I am pulling the value EventDate from a table called events. At the moment is giving me a timestamp along with the date - 2012-05-29 00:00:00.
Is there anyway to trim this in PHP?
Try using MySQL's build in functions such as DATE_FORMAT() or even DATE()
SELECT
DATE_FORMAT(EventDate, '%Y-%m-%d') AS eventDate
FROM events;
SELECT
DATE(EventDate) AS eventDate
FROM events;
If don't want to store time stamp in DB,then change field type from datetime to date.Then while retrieving you will get only date.
OR
$date = //store DB date in this variable;
date('Y-m-d',strtotime($date));
If you really need to do this in PHP, use the date() function.
$date = '2012-05-29 00:00:00';
echo date('Y-m-d', strtotime($date)); // 2012-05-29
Not sure if I should edit this question or just ask another question in a different post. I am having a hard time doing the reverse of what I originally asked. How do I get the "date number" in php. I want to assign the current date number to a variable to so I can use it in my query to compare what is already in the database.
I can not see the SQL Server database
but I am connecting to it using ODBC
and PHP on a Linux box. I was told
that the field "meeting_start" was a
"Date Number". When I get the value of
the "meeting_start" the format looks
like this.
2010-08-24 20:00:00.000
I need both the date and time but for
different parts of my function. Is
there a way to extract the data for
the date and save to a variable and
then extract the data for the time and
save to a different variable. Below is
me trying to take a stab at it.
$time_date_data = "2010-08-24 20:00:00.000"
$meeting_date = get_date($time_date_data);
$meeting_time = get_time($time_date_data);
You're looking for the date function:
$meeting_date = date('Y-m-d', strtotime($time_date_data));
$meeting_time = date('H:i', strtotime($time_date_data));
For $time_date_data = '2010-08-24 20:00:00.000', you'll get:
$meeting_date = '2010-08-24';
$meeting_time = '20:00';
You can change the format by changing the first argument to date() according to the docs...
Two options here. You can do it either on the SQL Server side or the application side.
For SQL: If you want the date only, in format YYYY/MM/DD, do this:
SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD]
If you want the time only, in format HH:MM:SS, do this:
SELECT CONVERT(VARCHAR(8), GETDATE(), 108)
Alternatively, you can do this all in PHP by using the "date" function, something like:
$theDate = date("YYYY/MM/DD", strtotime($theFullDate))
$theTime = date("HH:MM:SS", strtotime($theFullDate))
Try:
$stamp=strtotime("2010-08-24 20:00:00.000");
$date=date("Y-m-d",$stamp);
$time=date("H:i:s",$stamp);
or:
list($date,$time)=explode(" ","2010-08-24 20:00:00.000");
$meeting_date = substr($time_date_data, 0, -13);
$meeting_time = substr($time_date_data,11);
Using php I am inserting or updating the mysql database with create date or modified date using the variables
$datestring = "%Y:%m:%d %h:%i:%s";
$time = time();
$createdate= mdate($datestring, $time);
In this $createdate will be the variable I use to insert or update the table. But it's updating the wrong value. It's not the server time or localtime. Mostly it's 30 mins delay with the server's time.
Use date() function of PHP
$createdate= date('Y-m-d H:i:s');
Edit: after some googling it looks like you're using CodeIgniter. You should have mentioned that in your question.
The format string you're using doesn't match MySQL's date format. You want to use:
$datestring = '%Y-%m-%d %H:%i:%s';
use in mysql query like DATE_FORMAT(purchaseDate, "%Y:%m:%d %h:%i:%s") function