I have an ISO 8601 string like so:
2016-09-26T20:38:15.793Z
Now, I want to push the date forward 24 hours, but in the "Y-m-d H:i:s" format. I experimented with the following:
//$date1 is ISO 8601 string
date("Y-m-d H:i:s", strtotime($date1, '+24 hours'));
But unfortunately that's not working. Am I on the right track or completely wrong?
Thanks
Use the DateTime class. Make a new instance, add 24 hours to it and format it.
$dt = new DateTime('2016-09-26T20:38:15.793Z');
$dt->modify('+24 hours');
echo $dt->format('Y-m-d H:i:s');
https://3v4l.org/CHmmm
If you want to use strototime, you have the arguments in the wrong order, and you'll need to either convert your string to a timestamp first, which would require another application of strtotime, or concatenate it with the +24 modifier. It would need to be like this:
echo date("Y-m-d H:i:s", strtotime('+24 hours', strtotime($date1)));
or like this:
echo date("Y-m-d H:i:s", strtotime("$date1+24 hours"));
(I would also recommend using the DateTime class instead, but just FYI.)
Try using the sub method of the datetime class http://php.net/manual/en/datetime.sub.php
Related
I tried to use strtotime function to format the today's date in PHP but its giving me the wrong result. My code is given below.
<?php
$today = date("m-d-Y H:i:s");
echo date('m-d-Y H:i:s', strtotime($today));
?>
Here, I am getting this 01-01-1970 05:30:00 result.
Here, I need to get the proper datetime result.
date("m-d-Y") is what's causing issues for you. For example, take 01-02-2019 and 02-01-2019 - which is Februrary 1st and which is January 2nd? That format will make strtotime() return false, as it doesn't know what format that is for days that are greater than 12.
d-m-Y would be expected and a valid format.
You can use DateTime::createFromFormat() instead. Then you can create a valid DateTime object from that format, and use it however you need it to.
$today = DateTime::createFromFormat("m-d-Y H:i:s", date("m-d-Y H:i:s"));
echo $today->format("m-d-Y H:i:s");
Live demo
Documentation for DateTime::createFromFormat()
Alternatively, if you just need to print the date directly and not process it further, you don't need to go through any hoops and can just use date() as you were, without the second line. But you can not use that result in a strtotime() function, as it will return incorrect results.
echo date("m-d-Y H:i:s");
I have a string "3/6/2019 3:40:19 PM" that I want to convert to a Laravel/Eloquent datetime type.
Is there a way to do that without retrieve all small pieces of the string and rebuild again to the desired format?
You can use Carbon, which is already part of any Laravel install.
http://carbon.nesbot.com/docs/#api-getters
If you have a datetime string and you want to use it with a Carbon instance you can do:
$date = \Carbon\Carbon::createFromFormat('j/n/Y g:i:s A', '3/6/2019 3:40:19 PM');
Then you can do something like:
$date->format('Y-m-d')
$date->format('H:i:s')
One line solution:
date('Y-m-d H:i:s', strtotime('3/6/2019 3:40:19 PM')));
If you want to use Laravel's Carbon you can take a look at the api docs:
I've found an example similar to your situation that also parses the 'pm'
$date = Carbon::createFromIsoFormat('!YYYY-MMMM-D h:mm:ss a', '2019-January-3 6:33:24 pm', 'UTC');
echo $date->isoFormat('M/D/YY HH:mm'); // 1/3/19 18:33
Use the PHP strtotime() function.
strToTime($string)
And if you want to change the format you can use the date() function.
date('Y-m-d H:i:s', strtotime($string))
This saves you the hassle of installing new libraries and you can change the date format in any way you want in the date function
I store the date and time in mysql as a date/time field which has this format: 2012-03-12 14:51:26, what i am trying to do is simply rearrange the DD/MM/YY to look like this.
When i use the following code, it just gives me a date wrong format warning.
echo date_format($date, 'Y-m-d H:i:s');
If you are just displaying the date you can supply a certain format in the SQL query
SELECT DATE_FORMAT("%d/%m/%Y", date_column) FROM table
If you convert the MySQL timestamp to a unix timestamp, then you can use the date() function to output it in whatever format you like:
$unixTimestamp = strtotime($mysqlDate);
echo date($dateFormat, $unixTimestamp);
See the date format strings here: http://php.net/manual/en/function.date.php
First, convert it to a Unix timestamp (which I find to be all around better than a date_time field for a lot of reasons), then use PHP's date function.
echo date('Y-m-d h:i:s', strtotime($date));
Simply do:
date("d/m/Y", strtotime($date));
And read about strtotime function.
this will work.
$date = date_create("2012-03-24 17:45:12");
echo date_format($date, 'Y-m-d H:i:s');
I need to convert this date:
10.04.2011 19:00
To a date variable that I can use in PHP.
Can someone help me with that? I tried this way:
$dateConverted = date("d.m.Y H:i",strtotime ($date));
But it returns 01.01.1970 00:00
DateTime::createFromFormat() to the rescue!
It looks like your format is d.m.Y H:i.
So, this should work for you:
$dt = DateTime::createFromFormat('d.m.Y H:i', '10.04.2011 19:00');
echo $dt->format('Y-m-d H:i:s');
You should also take a look at the formats that strtotime and DateTime operate on. In particular, the reason that date didn't parse in strtotime is that it only expects dots as delimiters between Y, M and D if the year is only two digits. That's an odd one, don't look at me, it's not my fault.
$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