Web Tutorials

BIGGEST AND FASTEST Web Tutorials – live24u.com

img
Home / PHP / PHP Add Days Hours Minutes and Seconds to Datetime

PHP Add Days Hours Minutes and Seconds to Datetime

/
/
/
4 Views

PHP Add Days Hours Minutes and Seconds to Datetime

In this Post We Will Explain About is PHP Add Days Hours Minutes and Seconds to Datetime With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Add a duration or interval to a dateExample

In this post we will show you Best way to implement How to adds and substracts some days, months, years, hours, minutes, hear for Adds an amount of days, months, years, hours, minuteswith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

You can download source code and Demo from below link.




How to add Days, Hours, Minutes, and Seconds to Datetime in PHP

Add days to datetime using PHP.
Add date functions to hours to datetime using PHP.
Add date functions to minutes to datetime using PHP.
Add date functions to seconds to datetime using PHP.

<?php
	$dy = $hr = $min = 0;				
	$start_date = strtotime("2027-20-05 20:50:52");
	$start_date = date( 'd-m-Y h:m:s', $start_date);
	$date_string ="25 days, 25 hours and 25 minutes";			

	$date_string = str_replace("day","days",$date_string);
	$date_string = str_replace("hours","hour",$date_string);
	$date_string = str_replace("minute","minutes",$date_string);

	$dy = trim(substr(substr( $date_string, 0, strrpos( $date_string, ' day')), -2));
	$hr = trim(substr(substr( $date_string, 0, strrpos( $date_string, ' hour')), -2));
	$min = trim(substr(substr( $date_string, 0, strrpos( $date_string, ' minute')), -2));
	if($dy === "")
	{
		$dy = 0;
	}
	if($hr === "")
	{
		$hr = 0;
	}
	if($min === "")
	{
		$min = 0;
	}
	
	//add 2 day, 2 hour, 20 minutes and 30 seconds to time
	$end_date = date('d-m-Y H:i:s',strtotime('+'.$dy.' day +'.$hr.' hour +'.$min.' minutes',strtotime($start_date)));
?>

How to adds and substracts some days, months, years, hours, minutes

echo "<hr/>";
$firstStartDateTime = date("Y-m-d H:i:s");

//display the starting time using PHP
echo 'Starting Time: '.$firstStartDateTime;

//add 2 hour to time using PHP
$FinalDateTime = date('Y-m-d H:i:s',strtotime('+2 hour',strtotime($firstStartDateTime)));

//display the FinalDateTime time using PHP
echo 'FinalDateTime Time (added 2 hour): '.$FinalDateTime;

//add 2 hour and 20 minutes to time using PHP
$FinalDateTime = date('Y-m-d H:i:s',strtotime('+2 hour +20 minutes',strtotime($firstStartDateTime)));

//display the FinalDateTime time using PHP
echo 'FinalDateTime Time (added 2 hour & 20 minutes): '.$FinalDateTime;

//add 2 hour, 20 minutes and 30 seconds to time using PHP
$FinalDateTime = date('Y-m-d H:i:s',strtotime('+2 hour +20 minutes +30 seconds',strtotime($firstStartDateTime)));

//display the FinalDateTime time using PHP
echo 'FinalDateTime Time (added 2 hour, 20 minutes  & 30 seconds): '.$FinalDateTime;

//add 2 day, 2 hour, 20 minutes and 30 seconds to time using PHP
$FinalDateTime = date('Y-m-d H:i:s',strtotime('+2 day +2 hour +20 minutes +30 seconds',strtotime($firstStartDateTime)));

//display the FinalDateTime time using PHP
echo 'FinalDateTime Time (added 2 day, 2 hour, 20 minutes  & 30 seconds): '.$FinalDateTime;
echo "<hr/>";

You can download source code and Demo from below link.





Example

I hope you have Got What is How to add Days, Hours, Minutes, and Seconds to Datetime in PHP And how it works.I would Like to have FeadBack From My Blog(live24u.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(live24u.com) Are Most Always Welcome.

  • Facebook
  • Twitter
  • Google+
  • Linkedin
  • Pinterest

Vestibulum nec placerat orci. Mauris vehicula

Vestibulum nec placerat orci. Mauris vehicula,Vestibulum nec placerat orci. Mauris vehicula

It is main inner container footer text