This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 3 months ago.
I have a string that represent a date (ex. 01/10/2022 d/m/Y) and I want to add 90 days to this date, the problem I think I have is that php dont know how do add the 90 days to this date because is a string. I tried different solution posted by others but without a good result.
One example that I tried is:
$nextduedate = "01/10/2022";
$myDateTime = DateTime::createFromFormat('d/m/Y', $nextduedate);
$myDateTime = $myDateTime->format('d/m/Y');
$number_of_days = 90;
$str =' + '. $number_of_days. ' days';
$myDateTime = date('d/m/Y', strtotime($myDateTime. $str));
echo $myDateTime;
adn the result is 10/04/2022 istead of 01/01/2023
I know im missing something but I dont know what.
If you use Date Object, this script can help you.
You have to use "modify" function
<?
$startEntry = "01/10/2022";
$nbDays = 90;
$stringDiff = "+".$nbDays." days";
$startDate = DateTime::createFromFormat('d/m/Y', $startEntry);
$endDate = $startDate->modify($stringDiff);
echo $endDate->format('d/m/Y');
?>
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
how can I get and store date('2017-02-02') in php variable from result : datetime("2017-02-02 17:02:03").
Do you mean getting 2017-02-02 from 2017-02-02 17:02:03, well you have many options,
$date = date('Y-m-d', strtotime('2017-02-02 17:02:03'));
OR
list($date) = explode(" ", '2017-02-02 17:02:03');
OR
$arr = explode(" ", '2017-02-02 17:02:03');
$date = array_shift($arr);
OR
$dateObj = DateTime::createFromFormat("Y-m-d H:i:s", '2017-02-02 17:02:03');
$date = $dateObj->format("Y-m-d");
You need to convert the date to a timestamp with strtotime()
Example:
<?php
echo date("Y-m-d", strtotime('2017-02-02 17:02:03'));
?>
I hope this will help you!
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I need to convert year(YY) to YYYY. Here i am try to use date function in PHP but not getting the expected result.
$expiry_year = "30"; //year=2030
echo date('Y',strtotime($expiry_year)); //Result: 1970
echo date('Y',$expiry_year); //Result: 1970
Expected Result: 2030
Thanks To All!
Try this, use createFromFormat
$date = "30";
$dates = DateTime::createFromFormat('y', $date);
//now to get the outpu:
$arr = $dates->format('Y'); // output : 2030
DEMO
Try this :
$date = DateTime::createFromFormat('y', '30');
echo $date->format('Y');
Note:
If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).
For this you can use date_create_from_format Alias of the DateTime class createFromFormat method like as
$expiry_year = "30";
$date = date_create_from_format('y',$expiry_year);
echo $date->format('Y'); //2030
Docs
$expiry_year = "30";
$ce = substr(date('Y'),0,2); // Check the century
$ny = substr(date('Y'),2,2); // Check current year (2 digits)
if($expiry_year < $ny) {
$next_ce = $ce+1;
$output = $next_ce.$expiry_year;
} else {
$output = $ce.$expiry_year;
}
Something like that ? :D
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have to convert this string date 2016-09-26 00:00:00.000 to the yyyy-mm-dd format without other characters.
Can you help me, please?
You can just use the DateTime class along with the format() method:
$d = new DateTime('2016-09-26 00:00:00.000');
echo $d->format('Y-m-d');
Try this:
$datetime = '2016-09-26 00:00:00.000';
$date = date('Y-m-d', strtotime('2016-09-26 00:00:00.000'));
you will get the only date part in 'yyyy-mm-dd' format.
$test = strtotime('2016-09-26 00:00:00.000');
$test1 = date('Y-m-d h:i', $test);
$array1 = explode(' ', $var);
$date_temp = $array1[0];
This question already has answers here:
Time calculation in php (add 10 hours)?
(7 answers)
Closed 9 years ago.
Want to add hours in a date .
Please help.
The PHP code is:
$createdDate=date_create("2013-03-17 07:11:00");
$hour = 4;
I tried using strtotime. But it gives an error.
Thanks
Use DateTime modify method.
$date = new DateTime("2013-03-17 07:11:00");
$date->modify("+4 hours");
echo $date->format("Y-m-d H:i:s");
DEMO.
You could use DateInterval:
$iv = new DateInterval("PT{$hour}H");
$createdDate->add($iv);
// $createdDate is now modified
You can use date_modify:
date_modify($createdDate, "+4 hours");
This question already has answers here:
MySQL week calculation between two dates
(2 answers)
Closed 10 years ago.
I am creating a Top 30 Music Chart..
Hw do I get the number of weeks between two mysql formatted datetimes? like
From: 2013-01-15 11:41:14
Current Datetime: 2013-02-25 13:41:14
Using SQL is better, but in php it'll be something like this:
<?php
$datetime1 = new DateTime('2013-01-15 11:41:14');
$datetime2 = new DateTime('2013-02-25 13:41:14');
$interval = $datetime1->diff($datetime2);
$diff = $interval->format('%d');
echo (int)$diff/7;
Subtract the times using strtotime
$difference = strtotime($first_date)-strtotime($second_date);
$weeks = round($difference / 604800 );
<?php
$db_time_a = strtotime('2013-01-15 11:41:14');
$db_time_b = strtotime('2013-02-25 13:41:14');
$seconds_in_between = $db_time_a - $db_time_b;
$hours = (int)($seconds_in_between/60/60);
$minutes = (int)($seconds_in_between/60)-$hours*60;
$seconds = (int)$seconds_in_between-$hours*60*60-$minutes*60;
echo 'The time in seconds in between the two times is: '.$seconds_in_between.' (Hours:'.$hours.', Minutes:'.$minutes.', Seconds:'.$seconds.')';
?>
You can just chunk down the seconds with "remainder" and find out the minutes (and it's remaining seconds) and so on until you hit the time-distance you want.
does the WEEK() function helps you in your query ?
I don't know Mysql very well but lets assume the following :
select WEEK(date1-date2) from mytable where name='Ken';
You can use strtotime() method, then count difference between current time and time you have in your MySQL database.
Try DATEDIFF(date1, date2) function in mysql.
DATEDIFF() will return you the no of date between two given date. Hope you can easily get the weeks from output.