In database I am storing date value unix timestamp value for e.g.'1434952110' using time() now I am trying to compare todays value with this value as below
$jobpostdate = date("Y-m-d");//today's date passing in database to compare
query
$sql = "SELECT jsp_title, jsp_subtitle, jsp_desc, jsp_location, jsp_date ";
$sql .= "FROM js_projects WHERE DATE(jsp_date) = '$jobpostdate' ";
I tried above query , but even if the value is present I am getting no rows found, where i am going wrong how can I compare date values ? I know how to compare in php but i don't know exactly how to check in query ,Pls any one can help
If I understand correctly you have a unix timestamp in a varchar field and you can't change this.
If you compare the unix timestamp directly you will only get results that match the exact second of the timestamp.
You can use FROM_UNIXTIME() to convert the timestamp in a date value you can actually use:
WHERE DATE(FROM_UNIXTIME(jsp_date))=CURDATE()
This compares the date portion of the timestamp with the current date, giving you all database entries of the given day.
Note: This could create performance problems, since a lot of conversions occur on every request. You should really convert the unix timestamp in the database into a DateTime or Date field.
You could use PHP DateTime class.
$today = new DateTime('now'); // Can be what ever you want, not only now
// see below for reference.
$jobpostdate = $today->getTimestamp();
and query:
$sql = "SELECT jsp_title, jsp_subtitle, jsp_desc, jsp_location, jsp_date ";
$sql .= "FROM js_projects WHERE jsp_date = '$jobpostdate' ";
!PS I can't test it, but in a nutshell it should work.
Fore more reference about this awesome class go here:
http://php.net/manual/en/class.datetime.php
Related
I have a Database with column name DT, that has values:
1. 16-05-2015
2. 16-05-2015
3. 30-06-2015
4. 30-06-2015
Sql query in php that I am using is:
$sql = "Select * from logs WHERE (DT<= '$to') AND (DT>= '$from') ";
I have also used query:
$sql = "Select * from logs WHERE (DT BETWEEN '$to' AND '$from') ";
Both the queries are not functioning in the same way as required.
If $to=31-05-2015 and $ from = any date before 31st, the query displays the 30-06-2015 date as well, but it is not in the range.
When $to=30-05-2015, correct results are displayed but choosing any date after 30-05-2015 does not display the correct range, what could be the reason of it?
You have to change your date format to yyyy-mm-dd while saving it to the database and change it to the dd-mm-yyyy while displaying, and also change the datatype of the column to the datetime to make you query work
$newdate = date("d-m-Y", strtotime($yourdate));
apparently, DT is not configured as date in your DB.
And thus the values are ordered lexigoraphically, as strings.
(In lexigoraphical order: 30-... is before 31-..., no matter what ... might be)
You can either change your data to be saved as yyyy-mm-dd strings.
Or configure the column correctly as date
Basically i'm doing a simple wedding planner and i am trying to check whether the date that the user has inserted as available or whether the venue is booked. Basically, it's falling over with my SQL query. Initially i am setting the time:
$time = mktime(0, 0, 0, $month, $day, $year);
Then i have an sql query where i am checking it against an id variable that is previously set and the date that is passed through.
$sql2 = "SELECT * FROM venue_booking WHERE date_booked = ".$time." AND venue_id =".$id;
In this case the date in the database is 2015-01-01 and the date i'm passing through is 2015-01-01, I am checking this in an if statement if the amount of rows returned from the database is greater than 0 then echo booked, else echo available.
Even if the output is meant to say booked it still says available. Is there an issue with the way i am checking the time against mysql's date.
date_booked - This is a MySQL date (2014-01-01)
Anybody have any ideas?
Your current query is missing quotes around your date string so it wouldn't work as it is.
But to answer your question, just pass a valid date string in YYYY-MM-DD format and your query would work:
$date = $_POST['date'];
// Put date validation code here. I.e. make sure it is in YYYY-MM-DD
// format, etc. Might as well escape it, too since you aren't using
// prepared statements.
$sql2 = "SELECT * FROM venue_booking WHERE date_booked = '".$date."' AND venue_id =".$id;
I should also mention that you should probably switch to using prepared statements as it will make using user-provided data in queries safer.
Here is a possibly useful example of date validation. If you need to convert the date from one format to another, this will show you how.
mktime() returns a PHP timestamp, which is a unix timestamp, which is "seconds since the epoch", aka Jan 1/1970. You cannot directly compare that against a mysql date field. You'd literally be doing
WHERE '2014-01-01' = 12345678
Try
$ts = date('Y-m-d h:i:s', mktime(0,0,0,$month, $day, $year));
$sql = "SELECT ... date_booked = '$ts'";
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);
Hi i'm inserting a date entry into a field that has a Type Timestamp and field default CURRENT_TIMESTAMP ...my insert value will look like this
'.($data[16] == '' ? CURRENT_DATE() : $data[16]).'
how would I format the CURRENT_DATE() to be 30-Nov-10 to match what $data[16] format would be if not present. Also should I change the Field Type to Date and not Timestamp?
date($format,strtotime(CURRENT_DATE)) works for me.
CURRENT_DATE() is a MySQL function. You could use it as a string literal:
$sql_date = ($date == '') ?
"DATE_FORMAT(CURRENT_DATE(),'%d-%b-%y')" : "'$date'";
mysql_query("UPDATE foo SET date=$sql_date");
This assumes that $date is safe for SQL insertion.
Or you could just use PHP:
if (!$date) $date = date('%d-%M-%y');
mysql_query("UPDATE foo SET date='$date'");
Personally, I would generally use the PHP solution as it makes it easier to use the same query with the same safe parameter placeholders (not used in this example) regardless of how you build the date.
Also should I change the Field Type to Date and not Timestamp
If the time is irrelevant, then it should be a date field. However, if it's a date/timestamp field (and not a text field), you should be inserting in the YYYY-MM-DD format, and the above code is useless.
That is, if the field is text, then the above code could be useful. However, if you are storing simply a date into a single field, you should use a date field, In that case, the format you insert should always be in YYYY-MM-DD. When you retrieve the data, you can format it in the way you want for display.
Well without any further information
all i can tell you is
you should look into the function date and it's second arg
where you put a timestamp which you could create with mktime alt. strtotime
This question doesn't make sense.. I'm assuming you want to output the current date.. in which case you can change the format with the following:
$today = date("M/d/y");
also, time() will output the current date to the second in a unix timestamp
$today = time();
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')