Menu Home

date_interval_create_from_date_string

osbo.com | PHP | Functions | date_interval_create_from_date_string

Sets up a DateInterval from the relative parts of the string

Syntax

date_interval_create_from_date_string ( string $datetime ) : DateInterval

Parameters

datetime

A date with relative parts. Specifically, the relative formats supported by the parser used for strtotime() and DateTime will be used to construct the DateInterval.

Return

Returns a new DateInterval instance.

Examples

1

<?

$datetime = "1 day";

$return = date_interval_create_from_date_string($datetime);

echo date_interval_format($return, "%d");

?>
1

2

<?

$second = date_interval_create_from_date_string("1 second");
$minute = date_interval_create_from_date_string("1 minute");
$hour = date_interval_create_from_date_string("1 hour");
$day = date_interval_create_from_date_string("1 day");
$week = date_interval_create_from_date_string("1 week");
$month = date_interval_create_from_date_string("1 month");
$year = date_interval_create_from_date_string("1 year");

$format = "Y-m-d H:i:s";

$date = date_create();
echo date_format($date, $format) . PHP_EOL;

$date = date_create();
date_add($date, $second);
echo date_format($date, $format) . PHP_EOL;

$date = date_create();
date_add($date, $minute);
echo date_format($date, $format) . PHP_EOL;

$date = date_create();
date_add($date, $hour);
echo date_format($date, $format) . PHP_EOL;

$date = date_create();
date_add($date, $day);
echo date_format($date, $format) . PHP_EOL;

$date = date_create();
date_add($date, $week);
echo date_format($date, $format) . PHP_EOL;

$date = date_create();
date_add($date, $month);
echo date_format($date, $format) . PHP_EOL;

$date = date_create();
date_add($date, $year);
echo date_format($date, $format);

?>
2021-06-21 04:22:22
2021-06-21 04:22:23
2021-06-21 04:23:22
2021-06-21 05:22:22
2021-06-22 04:22:22
2021-06-28 04:22:22
2021-07-21 04:22:22
2022-06-21 04:22:22

PHP