Comparing datetime in SQL [duplicate] - php

This question already has answers here:
convert php date to mysql format
(9 answers)
Closed 6 years ago.
As suprising as it may sound, I couldn't find this exact information anywhere else. I need to compare 2 dates with time in a single SQL query. One of the datetimes is the exact end of a promotion and the other one is the datetime of the query (current datetime). My SQL query looks like this now:
$usr_id=$_SESSION['usr_id'];
$now = date("d-m-Y H:i:s");
$sql="SELECT * FROM usr_offers WHERE usr_id='$usr_id' AND of_enddate>='$now'";
This query does not work neither does show any kind of errors - it outputs all data from the table.
So how do I compare two datetime dates with time in them in a SQL query?

The default datetime format of mysql is "Y-m-d H:i:s"
Change your datetime format to
$now = date("Y-m-d H:i:s");
and try again.

Check your date format stored in the database. If it is stored as date time then change the format as
$now = date('Y-m-d H:i:s');
or it may stored as time string then convert the current time into the string and then compare the date.

Related

How to change date from database to dd/mm/yyyy - php in codeigniter [duplicate]

This question already has answers here:
Change date format (in DB or output) to dd/mm/yyyy - PHP MySQL
(9 answers)
Codeigniter Change Fetched Date format in View
(2 answers)
Closed 4 years ago.
MySQL stores the date in my database (by default) as YYYY-MM-DD The field type for my date is DATE (I do not need any time storage). Is there a simple way to change it by default to DD/MM/YYYY?
The internal storage format of dates in MySQL is usually out of scope for developers and most users of the database. Basic good practice is to always store date information using a proper date column, such as datetime. With regard to how to view your date information in different ways, you may use the DATE_FORMAT function. For example:
SELECT DATE_FORMAT('2014-02-01', '%d/%m/%Y') AS new_date
FROM dual;
01/02/2014
Demo
Simply use date() and strtotime()
date('d/m/Y', strtotime('now'));
OR
date('d-m-Y', strtotime('now'));
OUTPUT
06/06/2018
06-06-2018

Insert date in specific format into MySQL db

I need to insert the current date in the following format into a TIMESTAMP column in a MySQL db: d-m-Y
As of now I am using SQL NOW(), which returns the date as Y-m-d. Because I am using AJAX to display the data I cannot format the returned result using $date_returned->format(d-m-Y). Therefore I need to insert the date in the format that I will display on my AJAX call.
I tried to insert the date using the following functions:
1) date('d-m-Y');
2) (new \DateTime())->format('Y-m-d');
I understand these two functions do pretty much the same thing but I was not sure what else I should try.
MySQL threw the following error for both dates:
Error : (1292) Incorrect datetime value: '-2014' for column 'msg_date' at row 1
I am guessing this should be an easy fix but I can't figure out what is wrong.
I tried both TIMESTAMP and DATETIME on MySQL's end but neither worked. (I need it to be TIMESTAMP though).
Any suggestion is welcome!
$newdate= date('Y-m-d', strtotime('10-09-2015'));
or if you want current time just use
$now = date('Y-m-d');
If your msg_date column's structure is DATETIME or TIMESTAMP, the date format should be:
YYYY-MM-DD HH:MM:SS
which can be formatted through PHP like this:
$date = date("Y-m-d H:i:s");
Or if you already have a date, and you want it to convert to that format, we can use strtotime():
$date = date("Y-m-d H:i:s", strtotime($date));
For more date format, check this link.
The MySQL error message indicated that you had the date format the wrong way around.
Year must go first, then month, then day, as in:
date('Y-m-d') // right
In your first example, you have
date('d-m-Y') // wrong
In one of your examples above, you have it right, but you say you got the same response, so I assume that was not what you actually tried.
Another thing to note is that a MySQL TIMESTAMP column stores both a date and time. It's valid to give MySQL just a date (MySQL will just leave the time at zero), but if you have no need to store a time, you may as well make the column DATE instead of TIMESTAMP.
If you want to display your dates as d-m-Y then by all means do so, but they need to be sent to MySQL as Y-m-d.

SQL,PHP: Converting Date Format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I'm having a bit of a problem where both the SQL function and the php function does not convert dates properly when they're in the format of "DD-MM-YYYY". I am using a mysql database and I want to convert the date to "YYYY-MM-DD" before it is entered into the database.
The SQL function:
INSERT INTO lease
VALUES(3, 4, STR_TO_DATE('22-02-2015', '%Y-%m-%d'), STR_TO_DATE('27-02-2015', '%Y-%m-%d'))
Returns as "2022-02-20" and "2027-02-20"
Also, the php function puts the month and day in the wrong place so I have to do "Y-d-m" instead, like so:
$startdate = date("Y-d-m", strtotime('27-02-2015'));
$enddate = date("Y-d-m", strtotime('23-02-2015'));
Although that stores it correctly. Any date where the day is > 12 will reset the date to"1970-01-01" because it thinks the day is the month.
Can anyone help with this?
The format argument for STR_TO_DATE() is the format used for the string argument
So use
STR_TO_DATE('22-02-2015', '%d-%m-%Y'),

PHP check if 1 day passed [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP:find day difference between two date(“YmdHis”) reture
What I want to do is to get day from database, current date. And check if difference between them > 1 day:
$curdate= date("Y-m-d H:i:s");
$dbdate is value stored in datetime format in db.
$dif=$curdate-dbdate;
How to check if $dif>1 day ??
Assuming the stored date is expressed in the same time zone as the server, you can convert it to a timestamp using strtotime, and compare it to strtotime("-1 day"):
if (strtotime($dbdate) < strtotime("-1 day"))
frobnicate();
You can get just the day from each date.
$day = intval($curdate= date("d"));
This will get the day as an in. Do the same for the time of the data base and you get two integer representing the day. Using that you can calculate how many days have pass.
Beware that the last line should look like this:
$dif = abs($curdate-$dbdate);

php formatting dates for mysql [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
convert php date to mysql format
I have a date field which lets users type in their birthday in m/d/Y format (for example 06/01/1982) but just in case I also made my validation accept month and day values without any leading 0 (6/1/1982).
How can I convert these dates to Y-m-d for use in MySQL?
Other valid dates are 6/01/1982 and 06/1/1982.
You could use date() and strtotime() together like such:
$date = date('Y-m-d', strtotime('06/01/1982'));
That will give you
1982-06-01
I would validate all input using strtotime (which will handle all major date formats).
On the insert, cast it to the proper format using the inverse: strftime:
$userValidatedDate = strtotime($_GET['date']);
//...
$sqlDate = strftime('%Y-%m-%d',$userValidatedDate);

Categories