Based on the definition given on php.net about function date(), I tried to use it in the code like this:
echo"The date is :".date("l, d/m/Y, h:i:s a", time());
But the timestamp doesn't get printed. Why is this? I am unable to understand this part of the function.
To print timestamp just use time()
echo time();
I understood the problem:
I had to written the code properly but since It was not displaying current date and time, I was confused. I set the time zone to Asia/Kolkata. Now its working fine.
Thank you everyone for help.
In My SQL Database I have a Timestamp Column with values like this one representing the Date of the last edit:
2015-01-17 08:55:34.000000
I want to compare the Date with the current date and when is the same day I want to echo Today and otherwise I want to Display the Date of the last edit:
$timefromdb = '2015-01-17 08:55:34.000000'
$edit = strtotime($timefromdb);
if($edit > $_SERVER['REQUEST_TIME']){echo "Today";}
else{
echo strftime("on %A, the %d %B %Y", $edit);
}
echo " at ".date('h:i',$edit)
It always Displays 01/01/1970. There must be a Problem with strtotime. I did a bit of research and it seems like my Timestamp Format isn't a valid one: http://php.net/manual/en/datetime.formats.php
Around the web are a lot of Questions about converting Timestamps but I just can't find the right one: I also got a bit confused by all the functions to convert date stuff.
So can someone Tell me how to get a valid Timestamp for using it in strftime and to compare it to the REQUEST_TIME.
Thanks in Advance!
UPDATE: As Always: The Problem sits in Front of the PC. I declared the Variable but never assgined the Timestamp to it :)
Chop off the .000000 from the date as it makes the date a format strtotime() cannot work with. There's several ways to do this. A simple substr is one of them.
$timefromdb = substr('2015-01-17 08:55:34.000000', 0, -7);
I'm not exactly understood you, but
try
1. compare gettype( $edit ) and gettype($_SERVER['REQUEST_TIME'])
2. not sure what $timefromdb will be more then $_SERVER['REQUEST_TIME'], because IMHO when user edited data, time of it action will me less then current time.
I searched the site already to see if this question has been addressed already and found no results, but if it has then I appologize and if you can link me to that thread I'll refer to that to solve my problem.
I am trying to save a date in MySQL using PHP and it doesn't seem to be working. The field in MySQL is the "date" type and when I read data from that field it shows as 0000-00-00, so I have tried saving the data as 2009-09-24 for example, but it doesn't seem to work. I have tried saving it with quotations to make it a string, and without quotations and I still can't seem to figure it out.
Does anyone know how I can format the date propely so I can save it in MySQL?
Thank you very much!
Try using the following where $t is a timestamp (time()) or nothing
function MysqlDate($t=0)
{
if ($t==0)
return date("Y-m-d H:i:s");
else
return date("Y-m-d H:i:s", $t);
}
var_dump(MysqlDate());
I have a simple script which takes an input date, formats it, and stores it in the database. I was using the strftime function in the following way:
$pdate = strftime('Y-m-d', strtotime($_POST['post_date']));
For some reason, this suddenly started returning 'Y-m-d'. Yes, it was returning the format string I was passing it as the first argument! No date information at all. I also tried doing this by passing it a straight-up unicode timestamp as the second argument, but it still just returned the format string. It was working fine until a few days ago.
Now I've switched it to use the date() function instead:
$pdate = date('Y-m-d', strtotime($_POST['post_date']));
Everything works fine now! I'm just wondering if anyone has any ideas why the strftime() function suddenly stopped working. It seems really strange and it's going to bug me all day.
Actually the question might be opposite :) I don't know how did it return correctly formated date, because generally it should be like this:
$pdate = strftime('%Y-%m-%d', strtotime($_POST['post_date']));
As it's described here :)
You are using wrong Date format in strftime parameter. Check strftime function. It should be like this:
$pdate = strftime('%Y-%m-%d', strtotime($_POST['post_date']));
and there are many other formats as well.
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;