In my localhost WAMP stack, I am having a database table which has a column "Birth-date" as a "date-time" column type.
I am running the below SQL for that table to get the birth-date of the current year and its unix timestamp value. No issues here.
SELECT dateValue,
DATE_ADD(dateValue, INTERVAL YEAR(CURDATE())-YEAR(dateValue) YEAR) "Date1",
UNIX_TIMESTAMP(
DATE_ADD(dateValue, INTERVAL YEAR(CURDATE())-YEAR(dateValue) YEAR)
) "Date2"
FROM base_date
For example I get the below output from the SQL.
DATEVALUE DATE1 DATE2
-------------------------------+-------------------------------+-----------------
December, 18 1980 00:00:00+0000 December, 18 2013 00:00:00+0000 1387305000
December, 20 1985 00:00:00+0000 December, 20 2013 00:00:00+0000 1387477800
The problem is that when I try to display that unix formatted timestamp value in PHP, the date is not shown correct. There is a change of 1 day.
echo gmdate("Y-m-d", "1387305000"); // Displays 2013-12-17 and not 2013-12-18
echo gmdate("Y-m-d", "1387477800"); // Displays 2013-12-19 and not 2013-12-20
The confusing part is that :
When I test this with SQL Fiddle values, it worked correct. The unix timestamp out of the SQL is different for the same date in my localhost mysql(5.5.24) and the version available in sqlfiddle.com(5.5.32).
Fiddle: http://sqlfiddle.com/#!2/25e032/3/0
What could be the difference? Is it related to timezone or sql version or something else?
My host has the timezone set on MySQL to their local timezone (Pacific Time), so a PHP time() value (GMT) put into the database and displayed as a MySQL time is off by 8 hours or so. So, it may be a matter of your host's MySQL configuration. Be careful about mixing PHP timestamps and MySQL timestamps.
You are using gmdate() which adjusts to GM timezone. What is the default timezone set for your PHP? Try changing gmdate() to date() and see if it works correctly.
date_default_timezone_get() -> http://www.php.net/manual/en/function.date-default-timezone-get.php
On the gmdate() documentation you can get an explanation, look at Example 1:
http://www.php.net/manual/en/function.gmdate.php
Related
In my code users post date in content. Which is may be in different formats the format which is used in majority auto sets in MySQL date time field format i.e. yyyy-mm-dd hh:mm:ss. My problem is most of them have typo mistakes or different formats which my code doesn't pick correctly and it returns date something like this 1970-01-01 05:00:00. I am in deep from this issue. Is there any function that auto corrects the date time even if there is a typo mistake in it and if time is not available it auto adds the time to it?
Here are some examples of different formats I get
30 September 2017 | 09 31 AM
29 September 2017 | 02:30 PM
27/07/2016 | 08:20 PM
19/09/2017| 01:32 PM
14-July-2017 03:31 PM
September 5 2017
April 7 2016 04:55 PM
Here is my current PHP code
$get_date = ""; //Date in text form
$show_dated = strtotime(str_replace('|', '', $get_date);
$get_date = date("Y-m-d H:i:s", $show_dated);
echo $get_date;
Try using date_parse() instead of strtotime(). I did this for a while, and it functioned better. Ultimately my solution was to build a custom parser based on the confused mess of user inputs. I eyeballed 2000 entries to develop a 'gold standard' set of results, and then fine-tuned an algorithm to match until it performed 100% correctly.
1473424835535
http://www.epochconverter.com/
using this which gives exact date
Result : GMT: Fri, 09 Sep 2016 12:40:35.535 GMT
but in PHP
$returnValue = date('d.m.Y H:i:s', 1473424835535);
Result : 22.12.48660 05:32:15
tried in mysql also
select FROM_UNIXTIME('1473424835535', '%Y %D %M %h:%i:%s %x')
not working
Note : This record is migrated from google api
How to rectify this issue or any method to rectify this issue? Any help will be appreciated.
Your timestamp seems to be in milliseconds, while both php's and MySQL timestamp implementations work on a seconds basis. Solution: divide your timestamp by 1000:
FROM_UNIXTIME(1473424835535/1000,'%Y-%M-%d %H:%i:%s %f')
As stated on the very site you linked to:
Assuming that this timestamp is in milliseconds
The PHP and MySQL manual states that the timestamp they take is "seconds since Unix epoch".
A quick way to determine whether or not you have a valid timestamp, as defined by the manual, is to count the number of digits. Abovementioned site has a common timestamp listed as well, which you can use for comparison.
FROM_UNIXTIME(1473424835535/1000,'%Y-%M-%d %H:%i:%s %f') is the way os selecting from Mysql and
Proper PHP Way is echo date('d.m.Y H:i:s', (int) 1473424835535/1000);
Thanks for Mark Baker and Shadow
Working with php and mysql.
In test.php file have this simple script:
echo date('Y m d h:i');
echo date_default_timezone_get();
DB::execute('insert into test (date) values (now()) ');
In the browser it displays GMT as a result of server date_default_timezone_get function and date like "2015 07 16 20:06". However in the DB the value is stored with 2 hours added to the date like it was in central europe time (2015 07 16 22:06).
I've checked the mysql variable TIME_ZONE is set to SYSTEM, so I think that the date stored in the DB should be the same as it is displayed from php server? I'm in central europe in the moment, is it possible that mysql knows this somehow and sets the date based on my computer system timezone?
when I run this i mysql
select ##time_zone, now(), utc_timestamp()
i receive:
SYSTEM | 2015-07-16 22:27:22 | 2015-07-16 20:27:22
Also I've see that other mysql variable SYSTEM_TIME_ZONE is set to CEST. maybe that's why i have date stored in central europe timezone? Even if in php i have different time zone and mysql TIME_ZONE is set to system?
Date field in the table is datetime format
Current server time is
[me#unknown ~]# date
Fri Feb 24 14:53:41 MYT 2012
When I echo date('Y-m-d H:i:s') the date time is 2012-02-24 06:53:46. The hour, minute & second is not same.
How do I get same date with server time in PHP?
Set the timezone for MYT before getting the date, e.g.:
date_default_timezone_set('Asia/Kuala_Lumpur');
Also see this example.
You probably need to set timezone
OK. So I have been trying to implement a timer. Now a very weird thing is happening and I can't understand why ?.
Basically I am trying to find the difference between the last access and the current time. I am storing the time of last access in the database. This value is according to the server time. But when I try the time() function of php it shows me values which are 5-6 hours behind the time that I have in the database.
For example: here is my code :
$t1= strtotime($played_row->timer); // Time from the database with CURRENT_TIMESTAMP
$t2= strtotime("now"); // Get the current time
It shows Year: 2012 Month: 01 Day: 21 - 05:28 pm for t2
and Year: 2012 Month: 01 Day: 21 - 10:28 pm for my timestamp values.
Can anyone tell my why is that ?
P.S: I am running the code on my computer itself.
At a guess I would say that your database and PHP are using two different timezone offsets.
Most likely this is a timezone issue: if you are in the Eastern timezone, you are 5 hours away from UTC right now. If one sytem is returning local time and another is returning UTC this is what you will see.
Try using date_default_timezone_set() to set the timezone in PHP that is used in your database.
date_default_timezone_set — Sets the default timezone used by all
date/time functions in a script
Alse see date_default_timezone_get() how to get ini-set timezone.