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

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'));

Related

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

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');

Get day number from Date format by php [duplicate]

This question already has answers here:
How to extract only 'Day' value from full 'Date' string?
(7 answers)
Closed 2 years ago.
I have a date Formate from database number 2020-11-15 I want to get only the day number. I tried the following.
$your_date = '2020-11-15';
$ar_day2 = date("D", strtotime($your_date)); //The value is (Sun)
I am looking for result '15'.
Is there way by php?
use small d
$ar_day2 = date("d", strtotime('2020-11-15'));
echo $ar_day2 = date("d", strtotime('2020-11-15')); // will give => 15
echo $ar_day2 = date("D", strtotime('2020-11-15')); // will give => Sun

I have the following date: 2010-04-19 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have the following date: 2010-04-19 .
I would like to convert this date strtotime format and convert "Y-m" format.
Example = "2018-5"
my code:
$date1= 'onlinearticles_'.$date;
$article_table= date('Y-m',strtotime($date1));
But it didin't work, how can I do this ?
you should add alphabetic characters later..
follow these steps;
$date = 2010-04-19;
$newDate = date("Y-m",strtotime($date));
$stringWithDate = "onlinearticles_".$newDate;
dd($stringWithDate);

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
?>

Categories