How to add days into date in php?

Here we’ll provide the simplest way to add days to date using PHP. In PHP, using dateadd() function you can easily increase date.

1. Code:

    <?php 
	function dateAddWithFormat($date, $day, $format){
		if (is_null($date) || trim($date) == '' || "0000-00-00 00:00:00" == trim($date)) {
			return null;
		}
		try {
			$d = date_create_from_format($format, $date);
			if (false === $d) {
				throw new \Exception ("Invalid date");
			}
		}catch (\Exception $e) {
			return $dater;
		}
		date_add($d, date_interval_create_from_date_string("$day days"));
		$str = $d->format($format);
		return (false != $str) ? $str : null;
	}
	
	echo(dateAddWithFormat("2022-06-27 01:23:22", 5, "Y-m-d H:i:s"));
    ?> 

2. Example:

Input:

"2022-06-17", 5, "Y-m-d";
"2022-06-27 01:23:22", 5, "Y-m-d H:i:s"

Output:

2022-06-22
2022-07-02 01:23:22