$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
Related
I'm working with an XML document that is returning variables and for some reason in a xml return the timestamp is formatted like this... 20180606T110000 ... why anyone would format it like that makes no sense to me; however, its what I have to work with. ITs formatted YYYYMMDD , the T is the split between date and time, HHMMSS. ITs set up in a 24 Hour clock that I also need to convert to 12 hr clock with am/pm
I need that formatted like 06/06/2018 11:00:00 AM.
Is there a way to do that via a date format (I know how to use date() but I don't know how to bring in that timestamp the way its formatted) or even separating it out into
$year = xxxx
$month = xx
$day = $xx
$Hour=xx
etc. etc. etc.
if need be.
I've briefly looked at php's date create from format ( date_create_from_format('j-M-Y', '15-Feb-2009') ) but dont fully understand how that works.
I've also thought about a split. I've also looked at chunk_split and wordwrap but its not even amounts of characters so that would be complex to create.
Any ideas?
The format you're working with is "XMLRPC (Compact)" format. This is fully supported by PHP (you can see a list of supported formats here). To get what you want, just use a combination of strtotime() and date().
$timestring = "20180606T110000";
$timestamp = strtotime($timestring);
echo date("m/d/Y h:i:s A", $timestamp);
You can use PHP DateTime to parse a datetime String with any format. Please view the Parameters format in the following link to understand how the "Ymd\THis" part works: http://php.net/manual/en/datetime.createfromformat.php
<?php
$time = "20180606T110000";
$date = DateTime::createFromFormat("Ymd\THis", $time);
// 06/06/2018 11:00:00 AM.
echo $date->format("d/m/Y h:i:s A");
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
How to convert this (in ISO8601 format): 2014-03-13T09:05:50.240Z
To this (in MySQL DATE format): 2014-03-13
in php?
try this
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime($date));
The complete date function documentation can be found here: http://php.net/manual/en/function.date.php
The PHP function "strtotime" does nothing else then converting your timestring into an unix timestamp.
Hope I could help :)
P.s.:
Just in case strtotime will return 0 try using this:
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime(substr($date,0,10)));
Since PHP 5.2.0 you can do it using OOP and DateTime() as well (of course if you prefer OOP):
$now = new DateTime("2014-03-13T09:05:50.240Z");
echo $now->format('Y-m-d'); // MySQL datetime format
There is no reason to use the inefficient time functions. The most efficient way is to simply extract the first 10 characters:
substr($date,0,10)
People, that are really coding for year ≥10000, can use:
substr($date,0,strpos($date,"T"))
Simply convert datetime description into a Unix timestamp using with strtotime and then five format using Date Formats
Try it will surely work for you.
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime($date));
For those using Carbon (php library), the parse() works quite well:
Carbon::parse($date)
https://carbon.nesbot.com/docs/
Today I have published an interitty/utils package that deals with, among other things, the ISO-8601 format and perhaps all permutations of this standard.
I hope it will help you too.
$dateTimeFactory = new Interitty\Utils\DateTimeFactory();
$dateTime = $dateTimeFactory->createFromIso8601('1989-12-17T12:00:00Z');
I have been looking online for this answer and have come up empty...I am extremely tired so I thought I would give this a go....
I have a variable that has a date from a textbox
$effectiveDate=$_REQUEST['effectiveDate'];
What I am trying to do is take this date and add the current time
date('Y-m-d H:i:s', strtotime($effectiveDate))
When I echo this out I get 1969-12-31 19:00:00
Is this possible? Can someone point me in the right direction?
I found a solution to my problem....
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate . $currentTime));
echo $currentDate;
This takes a date from variable in one format and takes the date from another variable in another format and puts them together :)
Thanks everyone for their time.....
DateTime::createFromFormat
would also work but only if you have PHP 5.3 or higher...(I think)
The effectiveDate string is not in a format that strtotime recognizes, so strtotime returns false which is interpreted as 0 which causes the date to be displayed as January 1, 1970 at 00:00:00, minus your time zone offset.
The result you see is caused by the entered date not being in a format recognised by strtotime. The most likely case I can think of without knowing the format you used is that you used the US order of putting the month and day the wrong way around - this confuses strtotime, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.
The most reliable format for strtotime is YYYY-MM-DD HH:ii:ss, as it is unambigous.
The date is just a timestamp, it is not object-oriented and i don't like it.
You can use the DateTime object.
The object-oriented best way is:
$effectiveDate=$_REQUEST['effectiveDate'];
// here you must pass the original format to pass your original string to a DateTimeObject
$dateTimeObject = DateTime::createFromFormat('Y-m-d H:i:s', $effectiveDate);
// here you must pass the desired format
echo $dateTimeObject->format('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.