Here is the code in PostgreSQL I am trying to run:
select DATE_ADD(whenbooked,INTERVAL 4 HOUR) from booking WHERE id = 12310;
OR I try to run this code:
select DATE_ADD('2010-11-19 01:11:22',INTERVAL 4 HOUR)
Both codes access date/time stored in the same manner:
2010-11-19 01:11:22
PostgreSQL error code is this:
ERROR: syntax error at or near "4"
it is referring to the '4' in the line 'INTERVAL 4 HOUR'.
I can't figure out what the issue is.
I need to compare a time/stamp (written in exactly the same format as above) with the stored time/date PLUS 4 hours.
So the desired end result is to return: '2010-11-19 05:11:22'.
If this can be done in PHP or directly in SQL?
Based on your comment
I am just typing these codes into PgAdmin.
It looks like you're actually using PostgreSQL not MySQL.
You can view your existing code running on PostgreSQL in action on PostgreSQL here which gives the same error as you get above.
To correctly work with dates in PostgreSQL, you can view a list of date functions on the PostgreSQL documentation site here
What you're trying to do is this:
SELECT TIMESTAMP '2010-11-19 01:11:22' + INTERVAL '4 HOURS';
I found a method that works for me in PHP:
$desiredEndTime = strtotime("2010-11-19 01:11:22 + 10 hours");
echo date("Y-m-d H:i:s", $desiredendtime);
Looks like it converts the date/time into a weird-looking integer. And then on the second line it converts it back into my desired format. Now I can just insert that back into SQL.
Thanks!
Convert it simply to timestamp with "strtotime" method and then you can do whatever you like with that
Related
Im using codeigniter and somewhere along the way I guess we setup a php_errors db or its something that codeigniter does automatically. Anyways there is no timestamps in this db all there is is a time column that reads out like this : 2015-10-04 01:22:0
Id like to query for any errors that occured in the last 5 minutes but have no idea where to begin when its based off human readable time like this
Hi you can use the date_sub function
$date = date_create('2015-10-04 01:22:0');
date_sub($date, date_interval_create_from_date_string('5 minutes'));
echo date_format($date, 'Y-m-d h:i:s');
link to date_sub function on PHP website
http://php.net/manual/en/datetime.sub.php
if you want to do this in mysql you can use SUBTIME()
this is just an example not sure if it works
SELECT SUBTIME(time,'1 1:1:5.0'); FROM TABLE_NAME
SUBTIME() on mysql site
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_subtime
The last line of my query will not run without "not a valid month" error. I am trying to convert a start time in GMT to local time using an offset in one of the columns, then compare this to a date I have in a variable. It works without using the to_char and offset.
Any insight would be appreciated.
AND to_char(CT.START_GMT + K.OFFSET/1440,'MM/DD/YYYY HH24:MM:SS') >= TO_DATE('$From_Date', 'mm/dd/yyyy')
AND CT.START_GMT + K.OFFSET/1440 >= TO_DATE('$From_Date', 'mm/dd/yyyy')
You don't need a TO_CHAR() conversion.. if CT.START_GMT is a DATE
If CT.START_GMT is not DATE/TIMESTAMP , then you need to do TO_DATE() with the right format.
have a database with data and time
example: 2013-06-04 08:20:00
need to convert that to
example: 1378478351000
so i can add that number to jquery script event calendar
when i use this php code
$exc_date = $row_Recordset1['exc_date'];
$exc_date = microtime(true) *1000 ;
echo $exc_date;
it works right but it shows me the current date and time not the date and time saved at database,
can somone please help , thanks
If you want to avoid the calculations in PHP, add a computed column using unix_timestamp and str_to_date to your query:
select (
unix_timestamp(str_to_date(TimeStrColumnName, '%Y-%m-%d %H:%i:%s')) * 1000)
as utime,
# ... the rest of your query as before
if the MySQL column is already a date/time field (instead of a string), this will do it:
select (unix_timestamp(TimeStampColumnName) * 1000) as utime,
# ... the rest of your query as before
Of course it does, as you overwrite the database values in the second line by putting microtime instead in it.
You will not be able to get the microtime, when you just have the format of date in the database - so you should use 0. microtime is returning the value of the actual microtime at the moment.
You should do something like that to get time + microtime:
$exec_date = strtotime($row_Recordset1[exec_date"]) . "000";
More information here. The problem with this is, that you will get it as a string, so you will need to convert it to an integer:
$exec_date = (int) $exec_date;
Hope it helps you.
Hi i'm using php5 and mysql.
I have a time like that 10:00, 10:45 ... and i shoult put it a mysql database in form 'hh:mm:ss'
I tryed in different way but nothing works.
What i try was:
$time= time('H:i:s', $mytime)
$time= time('H:i:s', strtotime($mytime))
$time= strtotime($mytime)
$time= strtotime($mytime.':00')
$sql = "INSERT INTO table SET `time`='$mytime';
Hint: be sure you are using proper Mysql field format
Another hint: do not use randomly picked PHP functions. At least try to read the function description in the manual. It can give you idea if this function suit your needs or not.
strtotime need a complete date as input and outputs an int. You don't need an int for mysql, you only need the string you have plus the seconds info:
$time = $mytime.':00';
Then insert time in db, as #Your_Common_Sense says, you need to user TIME datatype in order to insert a time in this format.
I have a column of type date (only date) in mysql. However, when I am using the following:
$Answer->dateCreated=date('d-m-y');
I'm getting an error
A non well formed numeric value encountered
Any idea??
MySQL's date format is yyyy-mm-dd, which in PHP would be date('Y-m-d'). Your format string is reversed and using 2 digit years instead of 4 - Y2k's old news by now... don't use 2 digit years anymore.
I just did this and it is working:
$Answer->dateCreated = strtotime("now");
Try:
define('MYSQL_DATE_FORMAT', 'Y-m-d H:i:s');
$Answer->dateCreated = date(MYSQL_DATE_FORMAT);
I understand you're not wishing to save the time, don't worry MySQL will ignore the time for these columns.
Try using $Answer->dateCreated=date('Y-m-d');