PHP/Mysql Query by readable time - php

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

Related

Change returning date from Propel in PHP

I have a Database and I use Propel to get the data.
Now I have the following problem:
I want to get the date from one Table and add 30 days on it.
So something like this:
$invoicePayDay = $invoice->getCreationDay()->add(30 days)->format('d.m.Y');
I know that I can read the month, day and year separately and can than add 20 days and create a date from it again. but properly there is a better way to do it with Propel like my example?
Return the date as a var than you can do
$date = date('d.m.Y', strtotime('+30 days', strtotime($returnDate)));

Using INTERVAL in PostgreSQL to add hours date/time

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

PHP time to Mysql database

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.

Inserting DATE TIMESTAMP Value to MySQL Using PHP

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

How to insert a proper date into a mysql query

I was using this query to fill my values:
mysql_query("INSERT INTO 'drivers'(coupon,loyalty,etairia,package,pump,date,merchant,public,private,
amount,plate,nonce)VALUES('".$_REQUEST['coupon']."','".$_REQUEST['loyalty']
."','".$_REQUEST['etairia']."','".$_REQUEST['package']."',0,NOW(),'".$_REQUEST['m']."
','".$_REQUEST['pu']."','".$_REQUEST['pr']."','".$_REQUEST['amount']."',
'".$_REQUEST['plate']."','".$_REQUEST['nonce']."');");
This is working fine, but with NOW() I have the server hour so I want to convert it to my local hour.
I found this on another question:
$date = new DateTime();
$date->setTimezone(new DateTimeZone('Europe/Athens'));
$fdate = $date->format('Y-m-d H:i:s');
I printed it and it returned the correct hour.
Finally I tried to put it inside the query instead of NOW() but when I run it it doesn't even make a row to my base.
This is my code now:
mysql_query("INSERT INTO `drivers`.`pay`(coupon,loyalty,etairia,package,pump,date,merchant,public,
private,amount,plate,nonce)VALUES('".$_REQUEST['coupon']."','"
.$_REQUEST['loyalty']."','".$_REQUEST['etairia']."','".$_REQUEST['package']
."',0,'".$fdate."','".$_REQUEST['m']."','".$_REQUEST['pu']."',
'".$_REQUEST['pr']."','".$_REQUEST['amount']."','".$_REQUEST['plate']."','"
.$_REQUEST['nonce']."');");
My php version is 5.5.9
To get local time:
echo date("Y-m-d H:i:s");
To get global time:
echo gmdate("Y-m-d H:i:s");
Or set your timezone something like this:
date_default_timezone_set('Europe/Athens');
print date('Y-m-d H:i:s')."\n";
I will suggest you, do not use mysql_.It is deprecated from the latest version of PHP.Use mysqli_ instead of this.
As has already been suggested, try using "date_default_timezone_set" and "date" to get the date in your local timezone.
I would also recommend a couple of other things:
Use mysqli instead of mysql functions as mysql functions are deprecated
Escape your strings! To avoid SQL injection use mysqli_real_escape_string on anything that comes from the request
I understand that your question is "what is wrong with this mysql query ?". The problem is that you don't see which error is produced by MySQL.
This case is known for PHP as a "WSOD" or White screen of death : nothing is displayed, generally because of some error setting (php function error_reporting).
If you take a look at this page, you will find a way to declare a error handler, which is a great time saver when programming PHP. You will also read the reason of your error and you then can explain it to us all. :-)
Check out UNiX_TIME stamp. It will store as a big int . It's basically seconds count from a particular date which the clock was set . It's a good way as it gives you flexibility in retrieving in any format you want. You can convert it in client side. Hope this helps
You can use date('Y-m-d H:i:s') or gmdate('Y-m-d H:i:s') to get the current date and time. You will need to make sure that your date column is set as a DATETIME type
I don't know if you still need it, but with the following code
$timezone = +1;
$date = gmdate("Y-m-j H:i:s", time() + 3600*($timezone+date("I")));
You can change the timezone as you want (for example my timezone is GMT + 1 where I am now) and with the date you also no need to worry about when daylight time changes.
For you is
$timezone = +2;

Categories