The Format DateTime it increase 1 year more - php

I'm use this code:
$my_date_time = DateTime::createFromFormat('m/d/Y H:i', '20/02/2018 00:51')->format('Y-m-d H:i');
echo $my_date_time;
the code should show me this: 2018-02-20 00:51
but show: 2019-02-20 00:51
it increase 1 more year.. why..?

I think you reversed the day and the month when using DateTime::createFromFormat.
Try this format:
d/m/Y
$my_date_time = DateTime::createFromFormat('d/m/Y H:i', '20/02/2018 00:51')->format('Y-m-d H:i'); echo $my_date_time;
Test online

Related

date conversion in CodeIgniter / PHP does not gives output

I have date in this type of format: April 1st 2017 and I want to convert it into this type of format: 2017/04/01 in my CodeIgniter code using php. I have used below posted piece of code but it is not working. Please solve the issue.
Code:
$date = DateTime::createFromFormat('m/d/Y', "April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
You can use strtotime() and date() php functions as
$newDate = date("m/d/Y", strtotime("April 1st 2017"));
Or in CodeIgniter
$date = DateTime::createFromFormat('j F Y - H:i', 'April 1st 2017');
echo $date->format('m/d/Y H:i:s');
Your format can be used in the constructor of DateTime. See accepted formats.
$date = new DateTime("April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
Outputs:
Date = 2017-04-01
If you want to use DateTime::createFromFormat(), you have to use the proper format
"F jS Y"
The format you specified for your date is incorrect.
It would convert '04/01/2017' but it does not suit
April 1st 2017.
Try instead: createFromFormat('F dS Y')
Explanation:
F - full textual representation of a month, such as January.
d - day
S - English ordinal suffix for the day of the month
Y - 4-digit representation of year
you can try this also
<?php
$date='22 march 2018';
echo date('m/d/Y', strtotime($date));
?>

Changed position from day and month converting strings to date

I am working on following issue in PHP.
I have these variables:
$fecha = "01-07-2017";
$hora = "11:30";
Now I want to create a date variable with format Y-m-d H:i.
For that I am using following code:
$newDate = date("Y-m-d H:i", strtotime($fecha.' '.$hora));
But I am getting the output:
echo $newDate = 2017-01-07 11:30
I need it to be: 2017-07-01 11:30
What am I doing wrong?
Use the DateTime API to parse your string(s) initially
$dt = DateTime::createFromFormat('d-m-Y H:i', $fecha.' '.$hora);
$newDt = $dt->format('Y-m-d H:i');
Demo ~ https://eval.in/827662
Change your code to:
$newDate = date("Y-d-m H:i", strtotime($fecha.' '.$hora));
Notice that I just switched around 'h' & 'd' in the date function.

PHP date increase

I've written a piece of code which converts a date to the specific format and increases it by 1 day.
<?php
date_default_timezone_set('Europe/Moscow');
$mehdate = "2011-11-25";
$mehdate = date ('d m Y', strtotime ('+1 day', strtotime($mehdate)));
echo $mehdate, "\n";
?>
But then I have to increase $mehdate by 1 day one more time.
And I cannot understand how to do that. I already tried
$mehdate = date ('d m Y', strtotime ("+1 day", $mehdate));
and
$mehdate = date ('d m Y', strtotime ('+1 day', strtotime($mehdate)));
again but it won't work because
strtotime($mehdate)
returns FALSE.
So, how can I increase the $mehdate which was already formatted?
Your issue can easily be resolved if you use DateTime class.
Try this:
$mehdate = new DateTime('2011-11-25');
$mehdate->modify('+1 day');
echo $mehdate->format('d m Y')."\n"; // Gives 26 11 2011
$mehdate->modify('+1 day');
echo $mehdate->format('d m Y'); // Gives 27 11 2011
date_default_timezone_set('Europe/Moscow');
$mehdate = "2011-11-25";
$mehdate = strtotime ('+1 day', strtotime($mehdate));
$mehdate = date ('d m Y', $mehdate);
echo $mehdate, "\n";
Result
26 11 2011
For all newbies like me there is a simple advice: don't use 'd m Y' format, you'd better operate with 'd-m-Y'.
Or you have to use DateTime class, as Object Manipulator advised.

How to add months to a particular date

I just hope this question won't be marked as a duplicate because I've seen similar questions on stackoverflow but they all talk about adding days to the date, The problem here is that i want to add some particular months to a particular date which is gotten from my database I've tried adding it using strtotime() but the date just returns 1st January 1970, the code looks like this
<?php echo date('jS F Y', strtotime("$date +1 month")); ?>
//This is the value of date
$date = $student->date;
How to I add months to this particular date? Please note that the date is a timestamp in my database.Thanks
You have a Unix timestamp, not an actual date. Here I use the DateTime class to create a datetime object using that Unix timestamp. Then I can add a month to it and format the output.
$date = new DateTime('#'.$student->date);
$date->modify('+1 month');
echo $date-format('jS F Y');
If you want to stick to using date() and strtotime() you would use this:
echo date("jS F Y", strtotime("+1 month", $student->date));
strtotime() would take the starting date as the second parameter and then how you wish to modify it as your first parameter.
You should check out the documentation here,
But the just of it is the $date->add function. It allows you to add any amount of time to a timestamp using a DateInterval. Its a little tricky to get used to but here are a couple of examples:
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
which outputs:
2000-01-01 10:00:30
2007-06-05 04:03:02
The date interval is formatted in years months days hours minuets seconds, simply put in the amount you want and it will add it, so in your case:
<?php
$date->add(new DateInterval('P1M'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
PHP's strtotime() function allows for a second parameter that allows you to set a relative date.
If you would like to add a month to tomorrow, here's how:
<?php
echo date("jS F Y", strtotime("+1 month", strtotime("2014-10-09")));
// returns: 9th November 2014

PHP Reduce days, hours and minutes from a datetime

Need to reduce no of days, hours and minutes from a datetime using php.
Datetime is of the format Y-m-d H:i:s
eg: Suppose datetime is 2013-03-20 14:20:00. How to reduce 2 days , 3 hours and 10 minutes from this such that it results in 2013-03-18 11:10:00.
<?php
$date = new DateTime("2013-03-20 14:20:00");
$dateIncremented = $date->sub(date_interval_create_from_date_string('2 days 3 hours 10 minutes'));
$finalDate = $date->format("Y-m-d H:i:s");
echo $finalDate;
?>
Allirght. An Alias. But Readable format
use the DateTime object :
$date = new DateTime('2013-03-20 14:20:00');
$date->sub(new DateInterval('P2DT3H10M'));
echo $date->format('Y-m-d H:i:s');
You must explore DateTime::sub and DateInterval, and about DateInterval format
echo \DateTime::createFromFormat('Y-m-d H:i:s', '2013-03-20 14:20:00')
->sub(new \DateInterval('P2DT3H10M'))
->format('Y-m-d H:i:s'); // 2013-03-18 11:10:00

Categories