Generate Datetime in the future without UNIX timestamp - php

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

Related

Why need to convert date from string to unix timestamp by using strtotime function

I am just beginner in Laravel and as I watching certain tutorials on YouTube and author to change
format of created_at and updated_at in the view changed from string to unix timestamp and would like to ask
why we need to convert date from string to unix timestamp with the use
of strtotime function. Can't we still use string rather than
converting it to unix timestamp.
Secondly, why exactly unix timestamp over date time. Are there any
advantages of unix timestamp over date time
You do not need to use it unless you are required. You will know the reason if you ever required it.
The purpose of the function as per the PHP manual is,
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
In summary, it converts the date given in English to the number of seconds in Numeric.

PHP: Convert epoch to MySQL DateTime format

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

Get current timestamp in PHP in phpmyadmin format

In phpadmin we can assign default value as current timestamp for a field.
How can we generate timestamp in similar format in php.
For eg.: 2014-09-07 03:18:35.000000
Pretty simple in PHP:
date('Y-m-d H:i:s', time());
Where time() outputs the current time measured in the number of seconds since the Unix Epoch. It'll be based on your server clock. You can replace time() with any other timestamp or PHP function that generates a timestamp, like mktime(), strtotime(), DateTime::getTimestamp(), etc.

How to calulate the difference between a MySQL timestamp and the current time in PHP

I'm trying to calculate the difference between a timestamp retrieved from a MySQL database and the current time.
Appreciate the help.
As mentioned by #RemoteSojourner, I got the current time in a UNIX timestamp format (which returns time in seconds), I got the timestamp from the DB (using an ORM) and converted that to a UNIX timstamp too and then subtracted the two timestamps.
$current_time = strtotime("now");
$last_access_time = strtotime($this->last_access);
$inactivity_duration = $current_time - $last_access_time;
This example makes the difference between now and one hour ago.
select timediff(now(), now() - interval 1 hour)
You can use the strtotime function to parse the MySQL timestamp into a Unix timestamp can be further parsed or formatted in the PHP date function.
Retrieve the datetime from mysql like this
SELECT UNIX_TIMESTAMP(`time_col`) FROM `tablename`...
AND compare it to time()

Convert MySQL UTC datetime to UNIX timestamp

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?

Categories