month with leading 0 getting wrong result from createfromformat [duplicate] - php

This question already has answers here:
DateTime converts wrong when system time is 30 March
(2 answers)
Closed 1 year ago.
I want to convert this date 2021-02 into 02-2021, so I did:
echo DateTime::createFromFormat('Y-m', '2021-02')->format('m/Y');
but this return 03/2021
and the correct result is 02/2021

you have to specify the day:
echo DateTime::createFromFormat('Y-m-d', '2021-02-someday')->format('m/Y');
for example:
echo DateTime::createFromFormat('Y-m-d', '2021-02-01')->format('m/Y');

Related

PHP DateTime diff returns wrong difference [duplicate]

This question already has answers here:
Using DateTime::diff() to return days between two dates
(2 answers)
Closed 4 years ago.
I calculate the difference of to queried dates. When the difference is lower than one month everything is correct. Here a example of a wrong result:
Query output:
$row['start'] = '2018-08-06';
$row['end'] = '2018-09-26';
Code:
$start = new DateTime($row['start']);
$end = new DateTime($row['end']);
$days = $start->diff($end)->format("%d");
Output:
$days = 20;
The DateTime difference is correct and the error only comes in when you are formatting the output. %d is the day of the month, not the total days in the DateInterval. These two datetimes are 1 month and 20 days apart. So %d only shows the 20 days part. %a should get you the total number of days.
A full guide to the format can be found here:
http://php.net/manual/en/dateinterval.format.php

Function to add 1 day to date using php [duplicate]

This question already has answers here:
adding 1 day to a DATETIME format value
(9 answers)
Closed 4 years ago.
I have tring that contain date
i.e: 2018-07-18
Is there any function that get the string and add 1 day, so the result should be: 2018-07-19
This should work:
$date = "2018-07-18";
$new_date = date('Y-m-d', strtotime($date. ' + 1 days'));

I want result in hours between two datetime [duplicate]

This question already has answers here:
PHP: producing relative date/time from timestamps
(10 answers)
How do I compare two DateTime objects in PHP 5.2.8?
(8 answers)
Closed 7 years ago.
Here, I want to get hours between two dates.
If I subtract time between two dates, I want 23 hours in a result.
My field's datatype is as datetime.
My code is like,
$usr_check_login = $this -> applib->get_any_field('users_attendance',array('user_id'=>$this->session->userdata('user_id'),'clock_out'=>'0000-00-00 00:00:00'),'clock_in');
$cur_time = date('d-m-Y H:i:s');
$clk_time = date('d-m-Y H:i:s',strtotime($usr_check_login));
$res = $cur_time - $clk_time;
$hours = $res / ( 60 * 60 );
echo $hours."<br>";
echo $cur_time."</br>";
echo $clk_time."</br>";
I am getting result like,
0.00027777777777778
23-01-2016 11:29:19
22-01-2016 11:02:39
Here I want result as 24 hours as the dates are changed.
What can be the solution?

Php get month number from string with date [duplicate]

This question already has answers here:
Get month of a given date
(6 answers)
Closed 7 years ago.
i have a string in this format '2015-4-28'
How i get month number for this string ( the value in this example is 4)?
Thanks
Another option would be to do this:
date('m', strtotime($date));
Change the m to an n to get the month without the leading 0.
With this:
<?php
$a = "2015-4-28";
$parts= explode("-", $a);
echo $parts[0]; // 2015
echo $parts[1]; // 4
echo $parts[2]; // 28
?>

Converting month name in given date into month number [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP date format converting
I have a date like 2012-september-09. How to convert this into 2012-09-09 using PHP?
You can try with strptime
$date = '2012-september-09';
$strp = strptime($date, '%Y-%B-%d');
$ymd = sprintf('%04d-%02d-%02d', $strp['tm_year'] + 1900, $strp['tm_mon'] + 1, $strp['tm_mday']);
$new_date = new DateTime($ymd);
echo $new_date->format('Y-m-d');
here's a Codepad
Try this
Change
<?php echo date('Y-m-d',strtotime('2012-september-09'));?>
To
<?php echo date('Y-m-d',strtotime('09-september-2012'));?>

Categories