date is not presented correctly - php

I insert my date with the Now() mysql function, and when I select the date I use this:
DATE_FORMAT(com.date,'%h:%i %d/%m/%Y ')
Basically, the output that I get is the same, no matter what time I enter the input (The date is correct, but the time is incorrect):
12:00 20/11/2011
How can I correct this?!?

Mysql set current timestamp when you use now() function. So it should be correct output when you query by
SELECT DATE_FORMAT(com.date,'%h:%i %d/%m/%Y ')
I assume your com.date column is a timestamp, not a date.

Related

PHP count mysql row with changed date format

I have next problem:
My table date format was: LIKE 2017-01-08 18:50:25 (with time).
When i use sql query like
'SELECT date FROM table WHERE date = "2017-01-08"'
My row was empty, i need COUNT all row with same (today) date WITHOUT TIME.
Note, i will not change INSERT date time!
Use DATE() to get the date portion of the datetime field and compare it to today. Use COUNT() to get the number of records that match your query.
SELECT count(*) FROM table WHERE DATE(date) = CURDATE()
You can also replace CURDATE() with NOW(), CURRENT_DATE(), and CURRENT_DATE
You can also use it in the following way
'SELECT date FROM table WHERE date_format(date,'%Y-%m-%d') = "2017-01-08"'
the date_format is mysql function which return date according to your pattern the above pattern only return the Y-m-d from the datetime
I hope it will help you
plz change your statement equal operator to greater than
'SELECT date FROM table WHERE date > "2017-01-08"'
as by default if time portion is not present then it is putting 00:00...

compare user entered date with the date field stored in database

I am creating a website where user can create entertainment programs on a particular venue.I need to check whether there is any program fixed on that particular venue for the start time and end time entered by the user.
I have 2 column names in database named starttime and 'endtime' which contains the value in the form of yyyy-mm-dd hh:mm:ss. I think I need to convert the value stored in the both fields in database to timestamp. But how to write the query.
eg:
SELECT *
FROM table_name
WHERE
timestamp(starttime)>='user entered value'
AND timestamp(endtime)<='user entered value '
Is this query possible.?
If "user entered value" is unix timestamp, you can use UNIX_TIMESTAMP(field) function to convert that field to timestamp. If it's not, you have to first convert it to timestamp using strtotime or similar function. Or you can go the other way, and use datetime value inside the query by converting string to datetime using STR_TO_DATE(date, format) MySQL function.
For instance, if user enters the date in YYYY-mm-dd hh:mm format, use following query:
SELECT * FROM table_name WHERE starttime >= STR_TO_DATE('2012-02-01 12:00', '%Y-%m-%d %H:%i') AND endtime <= STR_TO_DATE('2013-01-01 00:00', '%Y-%m-%d %H:%i')
Personally I like to use UNIX timestamps inside the query, but that's just my quirk.
Why not use unix time? It makes dealing with dates much simpler.
$current_time=time();
This creates an integer.
Your query:
SELECT * FROM table
WHERE starttime
BETWEEN starttime AND endtime

Getting values in between ordinary date and timestamp from mysql database

I have a standard mysql timestamp in this format 2011-11-14 20:06:24 . This timestamp will be added whenever a new record is added to the table with the name lead.
I have two input fields for the user to enter from date and to date in dd/mm/yyyy format. Once the user enters both dates and press a button the values will be passsed to other field to get the records from the table lead which are inserted between the time range.
I tried the below query but its not working
SELECT DATE_FORMAT(added_on, '%d/%m/%Y') as date
FROM lead
WHERE added_on BETWEEN "10/11/2011" AND "14/11/2011"
Use standard format for dates, datetimes and timestamps: '2011-11-14' and not '14/11/2011'.
Use single quotes, not double quotes.
If added_on is a timestamp, you should not use BETWEEN or you'll lose almost all records from the last day because '2011-11-14' will be converted to '2011-11-14 00:00:00'. Use this instead:
WHERE added_on >= '2011-11-10'
AND added_on < '2011-11-15' --- note the "< the next day"
or
WHERE added_on >= '2011-11-10'
AND added_on < ('2011-11-14' + INTERVAL 1 DAY)
You should read carefully the MySQL docs: TIMESTAMP properties page for how timestamps are handled (and auto-inserted, updated) in MySQL and the MySQL docs: Timezone support.
you have to convert the date first in php
you can use "date" function of php to covert the date format according to mysql, as follows:
$converteddate = date('Y-m-d',strtotime($yourposteddate));
This will return date in format, e.g. 2011-08-15 which can be understood by mysql and then use it as normal in mysql.
SELECT DATE_FORMAT(added_on, '%m/%d/%Y') as date
FROM lead
WHERE added_on BETWEEN DATE_FORMAT("20/11/2011",'%m/%d/%Y')
AND DATE_FORMAT("21/11/2011", '%m/%d/%Y');

Mysql select - Get the dates of timestamps

At the mysql table, there are stored values of timestamps (like 1265138145).
What i want is to display the dates (eg 27/2/2011,10/3/2011,15/3/2011, 16/03/2011 etc) which belong to these timestamps. Is this possible?
(but only display one time the date, eg if there is 1265138145 and 1265138140 then display only one time the date - which is 16/3)
There are a variety of ways of doing this, the FROM_UNIXTIME command probably being the easiest.
For example: SELECT FROM_UNIXTIME(<timestamp field>, '%d/%m/%Y');
I'm not sure what you mean about "only display one time the date", but using DISTINCT on the necessary column should help.
i.e.: SELECT DISTINCT(FROM_UNIXTIME(<timestamp field>, '%d/%m/%Y')); may be all you require.
From within MySQL, use ADDDATE and interval of unixtimestamp seconds to the epoch, e.g.
select adddate('1970-01-01', interval 1265138145 second)
then display only one time the date
Use DISTINCT in your query, e.g.
select distinct date(adddate('1970-01-01', interval 1265138145 second))
from tbl ..
Both queries above return the column as a datetime value, which you can apply default formatting to in PHP.
Note about using FROM_UNIXTIME - you get your local UTC offset added to the time, which is unlikely to be what you want, unless the data was populated using UNIX_TIMESTAMP in the first place.
FROM_UNIXTIME: Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone
<?php
print date("d/m/Y", $timestamp);
?>
http://us.php.net/manual/en/function.date.php
Fetch your data and use just date() function.
echo date('d/m/Y', $row['date']);
use FROM_UNIXTIME(unix_timestamp), FROM_UNIXTIME(unix_timestamp,format) http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
in mySQL statement

mySQL selecting records due within the next month

I am trying to select all records in a table which have a date between the current date and 1 month ahead.
The date is stored like this DD-MM-YYYY
And the query I have tried:
SELECT * from tablename WHERE renewalDate BETWEEN DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')) AND  DATE_ADD(DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')), INTERVAL 1 MONTH)
But this does not return the correct results.
Is the date stored in an actual date or datetime field? If it's in a char/varchar field, you won't be able to use the BETWEEN syntax, as mysql will just treat them as fixed strings.

Categories