How to insert a proper date into a mysql query - php

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;

Related

timestamp from both date & time inputs and compare php

I've been struggling to get an exact answer for this question. There are many that are close to what I'm wanting but seem to still be just off. The application of this is to ensure that a booking can't be made for a past date.
I have a form which has an input for time & another for date. Firstly, I wan't to take both of these inputs & convert them to a timestamp.
This code returns nothing
$time_date = sprintf("%s %s", $pDate, $pTime);
$objDate = DateTime::createFromFormat('H:ia d/m/Y', $time_date);
$stamp = $objDate->getTimestamp();
echo $stamp;
So I've have tried using something like this
$pDate = $_POST['pDate'];
$pTime = $_POST['pTime'];
$full_date = $pDate . ' ' . $pTime;
$timestamp = strtotime($full_date);
echo $timestamp;
But for some reason it is returning an incorrect timestamp. (i've been using an online converter) 02/06/2014 as date & 12:23am as time, is not 1401625380. This according to the converter is Sun, 01 Jun 2014 12:23:00 GMT.
Does someone have working code for returning a timestamp of both time & date inputs?
Secondly I want to compare this timestamp with a specified one & check to see if it is greater than. I've created a timestamp for my timezone with this
$date = new DateTime(null, new DateTimeZone('Pacific/Auckland'));
$cDate = $date->getTimestamp();
echo $cDate;
and will simply have an if statement which compares the two and echos the appropriate message.
I feel as though there are multiple question on here that are ALMOST what I'm wanting to achieve but I can't manage to get them working. Apologies for the near duplicate.
Note: I'm using ajax to post form data (if this could possibly interfere).
Your second code snipped is correct. Assuming it's in datetime format (Y-m-d H:i:s).
From php manual about strtotime():
Each parameter of this function uses the default time zone unless a time zone is specified in that parameter.
Check your PHP default time zone with date_default_timezone_get() function.
To compare two dates, be sure they both are in same time zones.
For datetime inputs I personally use jQuery UI timepicker addon.
you receiving the time and date in string format - so i don't believe the ajax can interfere.
as for your question:
first of all - find out what is the locale timezone of your server. you can do it by this function: date_default_timezone_get.
if the answer doesn't suit you - you can use its "sister": date_default_timezone_set, and change it to whatever value you need (like 'Pacific/Auckland' - see the documentation there). it is also recommended to return it to the original value after you finish your stuff.
i believe fixing your locale timezone will solve your issue.

Manage PHP time function

I am using godaddy hosting service and I can manage local time, I have to use the server default time that is America/Phoenix.
Even if i am using date_default_timezone_set("Asia/Kolkata");
function in my config file then also there is no difference in time and godaddy people are not ready to help me with, I am tired of calling this guys but no response, I hate them all.
Is there any means I can get my local time using any function or any external API?
I am using this code
//set time zone india
date_default_timezone_set("Asia/Kolkata");
$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;
$date = date('m/d/Y h:i:s a', time());
echo $date."<br>";
and the output that I am getting is
The current server timezone is: Asia/Kolkata05/14/2014 12:06:26 am
Even if it is 4:38 pm here...
date_default_timezone_set should work for PHP functions like date, however I'm going to hazard a guess that you're having problems with date/time elements in a database such as MySQL.
I know I've had similar problems before, when trying to get everything on UTC instead of Europe/London...
When you've established the connection to your database, be sure to run this query:
SET time_zone = 'Asia/Kolkata';
This, in addition to date_default_timezone_set, should solve your problems. However, if you're using DATETIME columns, then those will not be fixed. TIMESTAMP columns will be automagically fixed to the new timezone because they are saved as UTC internally and converted upon retrieval.
You shouldn't touch date_default_timezone_set. The proper way to do that would be to use DateTimeZone object. Something like this:
$now = new \DateTime('now', new \DateTimeZone('Asia/Kolkata'));
echo $now->format(\DateTime::ATOM);
So, the idea is that you create an object in a timezone of server and then convert it to your timezone

Php Date Function

I have tried using php date function() like as follows
$date=date('Y-m-d').' '.date('H:i:s');
echo $date;
the output displayed is 2013-04-03 09:04:02.. but my system is 02:49 pm...
What time is being displayed for me? I tried changing the internet timing even then I am getting the same answer ?
First off, it is not necessary to use the date function twice. This will do the same thing:
echo date('Y-m-d H:i:s');
Second, you need to set PHP's date.timezone. This can be done in the php.ini file, but it can also be done using the date_default_timezone_set function, like this:
date_default_timezone_set('Europe/Amsterdam');
The string that you have to put in can be found in the documentation.
It may also be worth noting that you can tell the date function to use any time. This is done by passing in a *nix timestamp as the second argument. For example:
// One week ago from now
echo date('Y-m-d H:i:s', time()-604800);
It will show server's time only. If possible compare with your server time. If you want to use local machine's time you need to go with JAVASCRIPT.
And another suggestion,
You don't have to use individually to display date & time. You can achieve this in a single statement like this.
$date=date('Y-m-d H:i:s');
You will get the same format 2013-04-03 09:04:02
check for your system timezone and your default timezone in php by opening phpinfo()

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

Categories