1

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

mike vorisis
  • 2,786
  • 6
  • 40
  • 74
  • `gmdate('Y-m-d H:i:s')` try this to get UTC – Chonchol Mahmud Nov 21 '16 at 09:29
  • `echo` the query generated by php and run directly in db and check what error you receive. Also what type is the column `date`? – Daniel Dudas Nov 21 '16 at 09:30
  • 1
    Your query is vulnerable to [SQL injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – simon Nov 21 '16 at 09:31
  • @DanielDudas the date is a timestamp variable (I don't know why but when I put my query to a variable is not echoing anything) – mike vorisis Nov 21 '16 at 10:10
  • @DanielDudas I print it finally this is what I get (the hour is correct) INSERT INTO `drivers`.`pay`(coupon,loyalty,etairia,package,pump,date,merchant,public,private,amount,plate,nonce)VALUES('','','','',0,'2016-11-21 12:26:07','','','','','',''); – mike vorisis Nov 21 '16 at 10:24
  • @mikevorisis Try to run this query directly in db using phpmyadmin or other mysql client. See there if it's inserted ok or you have some errors returned by mysql. – Daniel Dudas Nov 21 '16 at 10:57

6 Answers6

1

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.

Chonchol Mahmud
  • 2,717
  • 7
  • 39
  • 72
  • It was deprecated a LOT sooner than the latest version. It was deprecated in PHP 5.5 which was released mid 2013. All `mysql_*` functions were removed with PHP 7.0 late 2015 – bradynpoulsen Nov 21 '16 at 09:54
  • How do I parse them to my query? Thanks – mike vorisis Nov 21 '16 at 10:16
  • @mikevorisis `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.` have you get any error or any date/time in your database insertions or error ? – Chonchol Mahmud Nov 21 '16 at 10:20
1

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:

  1. Use mysqli instead of mysql functions as mysql functions are deprecated
  2. Escape your strings! To avoid SQL injection use mysqli_real_escape_string on anything that comes from the request
1

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. :-)

Community
  • 1
  • 1
b2vincent
  • 610
  • 7
  • 14
0

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

0

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

bradynpoulsen
  • 365
  • 3
  • 11
0

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;
Konstantinos Natsios
  • 2,874
  • 9
  • 39
  • 74