I have to store epoch timestamps into a MySQL database DateTime column. I need to be able to convert the epoch into the DateTime form to be able to add it to the database. The epoch data is grabbed from an external source so we have no control over that, the database is also established and should be left as it is. We just need to be able to convert between the two in PHP.
PHP
date("Y-m-d H:i:s",$epochTS);
MySQL
FROM_UNIXTIME(timestampColumn)
Nothing more to it
No need to do any convert for MySQL or PHP just or enter the same to database
echo date('r', $epoch);
strotime() is the function used to convert datetime to epoch. Documentation
strotime() is the function used to convert datetime to epoch. Documentation
time() is the function used to convert epoch to datetime. Documentation
Related
strtotime($_SESSION['starttime'])
I tried to convert the mysql time stored as $_SESSION['starttime'] of value 1422094831 but I want to convert the resulting to time using strtotime but returns no value. What could be wrong?
strtotime() is used to create a timestamp, not convert it to a date. If your session variable stores a timestamp, try using the date() function to convert it to a more readable format.
echo date("Y-m-d", $yourTimestamp); //Will print YYYY-MM-DD format date
The PHP doc will help you using the date() function correctly.
If that's not what you want to do then please provide more information in your question, from what I understand you already have a timestamp.
Lets say today is the 08.20.2014.
I want to get the date from "today" additional 30 years (08.20.2044) with PHP and insert it into my mysql Database with it´s Datetime field.
How do I correctly generate the YYYY.MM.DD H:i:s without using the UNIX timestamp?
If i use
mktime() -> I get a UNIX timestamp (Limited, has a maxyear 2038, so nonsense)
time() -> I get a UNIX timestamp
date() -> I need a UNIX timestamp
strtotime() -> Im converting from/to UNIX timestamp
Am I completly missing a point?
What is the sense of using DATETIME if I´m using the UNIX timestamp in my code which is limited (I know that DATETIME is also limited to the year 9999)?
Don't use unix timestamps in your code, use DateTime class.
$time = new DateTime();
$time->add(new DateInterval('P30Y'));
echo $time->format('Y-m-d');
it prints 2044-08-20
What would be the type of the variable returned if the type of variable in mongodb is datetime?
When I convert it into json, what will be displayed? Will it be a large number like unix time stamp or what?
Best to use mongos native JavaScript Date objects.
You can convert date objects to and from Unix timestamps using 'getTime()' method or 'Date(milliseconds)'
Remember the JavaScript Date object is measured in milliseconds due to the the Unix epoch.
The documentation says it's a UTC DateTime. So expect it to be a string such as:
2010-10-28T23:07:11Z
These both correctly return the current UNIX timestamp:
SELECT UNIX_TIMESTAMP(LOCALTIMESTAMP()); #MySql
echo time(); //PHP
But I'm storing UTC_TIMESTAMPs in my database (not LOCALTIMESTAMPs).
How can I convert a UTC datetime to a UNIX timestamp using MySQL?
Note that LOCALTIMESTAMP() is a synonym for NOW(). So what you're really asking is how to get the current time and convert it to GMT and then convert to a unix timestamp to store in the db. So this will work:
SELECT UNIX_TIMESTAMP(CONVERT_TZ(NOW(), ##global.time_zone, 'GMT'));
As an aside, it's always much better to use the time and date columns of a database rather than unix timestamps. It makes querying and displaying results much easier.
Update: Are you sure you are getting what you think you are? UNIX_TIMESTAMP returns a UTC based seconds since the UNIX epoch. It does not return a MySQL DateTime type. If you have an actual UTC DateTime instance, then you can put that directly into your DateTime column of your database and don't have to use UNIX_TIMESTAMP as an intermediary. What type do you actually have that's in local time?
I'm reading a datetime field from mysql db. I'd like to convert it in PHP:
from: 2009-05-23 14:46:23
to 05/23/2009 02:46 pm
Notice the am/pm conversion.
Thanks in advance..
// assume you retrieve the mysql date in variable $date
date("m/d/Y h:i a", strtotime($date));
Call me old fashioned. I store all dates as epoch, and if I will ever go outside my timezone, as epoch of UTC of the date.
Epoch is seconds since Jan 1st, 1970 or something like that.
Known in MySQL as Unixtime. FROM_UNIXTIME(), etc..
$epoch = strtotime($mdate);
You can also store dates directly in MySQL with data type DATETIME if you are so inclined.
Your chosen format is a drop in replacement actually.
Depending on the type that's return by your db code, you can create a new DateTime object or format an existing one with date_format(). From the example you give, the date format string should be "m/d/Y h:i a". Note that the conversion from 24h to 12h is handled in either case.
Have a look at the format options for DateTime:date() and DateTime:format() (date_format is an alias for this)