I would like to know if there is a function in php to generate data/time format like in mysql : 0000-00-00 00:00:00 .
I know you can generate data/time in the sql query with CURDATE() but I would like to know if it's possible to generate it with php in the same format mysql datatime format .
Sure is! Give this a go date('Y-m-d H:i:s')
At the moment PHP only supports RFC 2822 and ISO 8601 conform dates as well as Unix epoch timestamp. Check more here.
The reason for that is probably that they include timezone offset, and the "mysql form" - its a localized compbound format - doesnt, which would often lead to a mess in when using with application servers.
But you can define your own formats as you like, all the components are available via the datetime functions. Just have a look here and here
Example:
$mysql_date = date('Y-m-d H:i:s', $unix_timestamp);
But be aware of the Y2k38 bug if you are not on a 64 bit system, you may want to use the datetime class
This should convert the standard formats:
$format = 'DATETIME';
$format = str_replace(
array('DATETIME', 'DATE', 'TIME', 'YEAR'),
array('DATE TIME', 'Y-m-d', 'H:i:s', 'Y'),
$format);
echo date($format, time());
Related
I am not sure how to convert timestamp into ISO-8601 and also apply UTC.
However, I am looking for following output:
2017-09-23T01:08:36.6437128Z ( Format = ISO-8601. Timezone = UTC)
I have tried couple of ways to get this but I am not getting it so anyone know how to get this. As this must be very quick and easy fix but I am missing something and that's the reason I am not getting this output.
Any help would be appreciated:)
Thanks
First of all, your desired format is not ISO 8601 compliant because you're including microseconds.
Use date_default_timezone_set to get UTC and date to easily format a timestamp.
Demo: https://3v4l.org/aRKqi
$timestamp = 1506086214;
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// ISO 8601 (does NOT include microseconds)
echo date("c", $timestamp) . PHP_EOL;
// custom format e.g. 2017-09-23T01:08:36.6437128Z
echo date("Y-m-d\TH:i:s.u\Z", $timestamp) . PHP_EOL;
2017-09-22T13:16:54+00:00
2017-09-22T13:16:54.000000Z
Lets say I've got my Unix Timestamp of 1373623247. Now I understand that the timestamps are just seconds since X. The issue I have is the formatting of it.
The server I use is hosted in Germany, however I am in the UK so the output is 12-07-13 12:01:01, when actually its only 11:01:01 here.
The code I am using is as below:
$date = 1373623247;
echo date("j-m-y h:i:s",$date);
What I did was use date_create and timezone as follows:
$date1 = date("j-m-y h:i:s",$date);
$dateobj = date_create("$date1", timezone_open('Europe/London'));
echo date_format($dateobj,"j-m-y h:i:s") . "\n";
The issue I now have is that it's actually adjusted the date to tomorrow, and hasn't altered the time!
You do not need to involve date at all:
$date = 1373623247;
$dateobj = date_create_from_format("U", $date);
date_timezone_set($dateobj, timezone_open('Europe/London'));
echo date_format($dateobj,"j-m-y h:i:s") . "\n";
This code converts the timestamp directly to a DateTime instance using the U format specifier. It's both shorter and cleaner to not work with date, as you don't need to worry about the server's default timezone at all.
Pro tip: date and strtotime get much coverage, and in certain cases are very convenient, but DateTime can do everything on its own and IMHO results in code that is much more maintainable.
When I make projects on my website, I've saved the time and date that I made them on in MySQL as '2012-02-27 12:32:36' format.
How can I get this to display as:
"February 27, 2012 12:32 p.m."?
I've looked at other date formatting on PHP and read on the PHP website, but all of the functions they provide don't seem to be for grabbing a date in the format that I've saved it in.
First, convert the MySQL DateTime string you have into a DateTime object by using DateTime::createFromFormat()
$mysqlDateTime = '2012-02-27 12:32:36';
$date = DateTime::createFromFormat('Y-m-d H:i:s', $mysqlDateTime);
Then, simply call DateTime->format() with the desired format string:
$formattedDate = $date->format('F j, Y g:i a');
You can also use the MySQL UNIX_TIMESTAMP() date function (to extract a unix timestamp compatible with date()) or use strtotime(), however note that these methods do not support dates after January 19, 2038 on 32-bit systems due to overflow (the two methods do not support PHP's automatic number variables promotion).
Is there a php func that does this:
$timestamp = get_timestamp_from_str('d/m/Y H:i', '10/10/2012 10:10');
strtotime() will use the USA version for '10/10/2012' wich is m/d/Y, but i have the day first.
For a particular case i can make my own parser, but the date format can change depending on the visitors local settings. However, i will always know the current format.
I had to insert this last paragraph so that this question is long and good enough for this portals question quality filter
Thank you
Yes, it's called DateTime::createFromFormat().
You would use it as:
$datetime = DateTime::createFromFormat( 'd/m/Y H:i', '10/10/2012 10:10', new DateTimeZone('Something'));
$timestamp = $datetime->getTimestamp();
Just make sure your timezone string is in the list of supported timezones.
$data['user']['time'] = '2011-03-07 00:33:45';
how can we add 1 year to this date ?
something like $newdata = $data['user']['time'] + 1 year ?
or
$newdata = 2012-03-07 00:33:45
Thanks
Adam Ramadhan
strtotime() is the function you're looking for:
$data['user']['seal_data'] = date('Y-m-d H:i:s', strtotime('+1 year', strtotime($data['user']['time'])));
First, you have to convert the MySQL datetime to something that PHP can understand. There are two ways of doing this...
Use UNIX_TIMESTAMP() in your query to tell MySQL to return a UNIX timestamp of the datetime column.
SELECT whatever, UNIX_TIMESTAMP(myTime) AS 'myUnixTime' FROM myTable;
Use DateTime::createFromFormat to convert your string time to something PHP can understand.
$date = DateTime::createFromFormat('Y-m-d H:i:s', $data['user']['time']);
Once that is done, you can work with the time... Depending on the method you used above, you can use one of the following.
If you have a unix timestamp, you can use the following to add a year:
$inAYear = strtotime('+1 year', $data['user']['unixTime']);
If you have a DateTime object, you can use the following:
$inAYear = $date->add(new DateInterval('P1Y'));
Now, to display your date in a format that is respectable, you must tell PHP to return a string in the proper format.
If you have a unix timestamp, you can use the following:
$strTime = date('Y-m-d H:i:s', $inAYear);
If you have a DateTime object, you can use the following:
$strTime = $inAYear->format('Y-m-d H:i:s');
Alternatively, if you don't want to deal with all of that, you can simply add one year when you query.
SELECT whatever, DATE_ADD(myTime, INTERVAL 1 YEAR) AS 'inAYear' FROM myTable;
Current (2017) Practice is to use DateTime
This question is top on a google search for "php datetime add one year", but severely outdated. While most of the previous answers will work fine for most cases, the established standard is to use DateTime objects for this instead, primarily due strtotime requiring careful manipulation of timezones and DST.
TL;DR
Convert to DateTime: $date = new DateTime('2011-03-07 00:33:45', [user TZ]);
Use DateTime::modify: $date->modify('+1 year');
Format to needs.
Change the timezone with DateTime::setTimezone from the list of supported timezones: $date->setTimezone(new DateTimeZone('Pacific/Chatham'));
Convert to string with DateTime::format: echo $date->format('Y-m-d H:i:s');
Following this pattern for manipulating dates and times will handle the worst oddities of timezone/DST/leap-time for you.
Just remember two final notes:
Life is easier with your system timezone set at UTC.
NEVER modify the system timezone outside of configuration files.
I've seen too much code that relies on date_default_timezone_set. If you're doing this, stop. Save the timezone in a variable, and pass it around your application instead, please.
More Reading
How to calculate the difference between two dates using PHP?
Convert date format yyyy-mm-dd => dd-mm-yyyy
PHP - strtotime, specify timezone
I think you could use strtotime() to do this pretty easily. Something like:
$newdata = date('c', strtotime($data['user']['time'] . ' +1 year'));
Though the 'c' format string isn't the same as your input format. You could consult date()'s docs for how to construct the correct one.
'Y-m-d H:i:s' — as Tim Cooper suggests — looks correct.
This should do the trick (not tested).
$data = "2011-03-07 00:33:45";
echo 'Original date +1 year: ' . date('Y-m-d H:i:s', strtotime(date("Y-m-d H:i:s", strtotime($data)) . " +1 year"));
First-of-all if your date format is separated by a slash (/), like '2019/12/31' then you should convert it in dash (-) format, like '2019-12-31', to do so use str_replace() function.
$string = str_replace('/', '-', '2019/12/31'); //output: 2019-12-31
To add time/day/month/year do not use strtotime() function, because it can't add a time which is beyond year 2038.
So here I would prefer to use DateTime() function.
$string = '2000-01-01';
$date = new DateTime($string);
$date->add(new DateInterval('P60Y5M2DT6H3M25S')); //60 Years 5 Months 2 Days 6 Hours 3 Minutes 25 Seconds
echo $date->format('Y-m-d H:i:s'); //output: 2060-06-03 06:03:25