Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to add time in minutes in datetime string PHP?
In PHP, you can add minutes to a datetime string using the strtotime() function or the DateTime class. The strtotime() method provides a simple way to parse time expressions and add time intervals.
Syntax
$result = strtotime('datetime_string + X minute');
You can replace X with any integer value representing the number of minutes to add.
Using strtotime()
Here's how to add minutes to a datetime string using strtotime() ?
<?php
$addingFiveMinutes = strtotime('2020-10-30 10:10:20 + 5 minute');
echo date('Y-m-d H:i:s', $addingFiveMinutes);
?>
2020-10-30 10:15:20
Using DateTime Class
Alternatively, you can use the DateTime class for more object-oriented approach ?
<?php
$date = new DateTime('2020-10-30 10:10:20');
$date->add(new DateInterval('PT10M')); // Add 10 minutes
echo $date->format('Y-m-d H:i:s');
?>
2020-10-30 10:20:20
Adding Different Time Intervals
You can add various time intervals using strtotime() ?
<?php $originalTime = '2020-10-30 10:10:20'; // Add 15 minutes $add15Minutes = strtotime($originalTime . ' + 15 minute'); echo "Original: " . $originalTime . "
"; echo "Add 15 minutes: " . date('Y-m-d H:i:s', $add15Minutes) . "
"; // Add 1 hour $add1Hour = strtotime($originalTime . ' + 1 hour'); echo "Add 1 hour: " . date('Y-m-d H:i:s', $add1Hour) . "
"; ?>
Original: 2020-10-30 10:10:20 Add 15 minutes: 2020-10-30 10:25:20 Add 1 hour: 2020-10-30 11:10:20
Conclusion
Both strtotime() and DateTime class provide effective ways to add minutes to datetime strings in PHP. Use strtotime() for simple operations and DateTime for more complex date manipulations.
Advertisements
