calculate the time difference between two times in hours and minutes php

calculate the time difference between two times in hours and minutes php OR Add or Subtract days, month and year to a date using PHP

calculate the time difference between two times in hours and minutes php

Use Mathematical Formulas and Use date_diff() Function to Get Time Difference in Minutes in PHP Examples. get hours difference between two dates in php. Get Hours/Months Time Difference Between Two Dates In Laravel

$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

1. Add days to date

The following example shows how to add 7 days to date.

<?php
    $birthDt = "2022-11-30";
    echo date('Y-m-d', strtotime($birthDt. ' + 7 days'));
?>
 
// Result: 2022-12-07

Add 15 days to the current date.

<?php
    $birthDt = date("Y-m-d");
    echo date('Y-m-d', strtotime($birthDt. ' + 15 days'));
?>
 
// Result: 2023-01-21

Add 1 month to date.

<?php
    $birthDt = "2023-01-06";
    echo date('Y-m-d', strtotime($birthDt. ' + 1 month'));
?>
 
// Result: 2023-02-06

Add 1 year to date.

<?php
    $birthDt = "2023-01-06";
    echo date('Y-m-d', strtotime($birthDt. ' + 1 year'));
?>
 
// Result: 2021-01-06

2. Subtract days from date

how to subtract 5 days from date.

<?php
    $birthDt = "2023-01-04";
    echo date('Y-m-d', strtotime($birthDt. ' - 5 days'));
?>
 
// Result: 2022-12-30

Subtract 2 days from current date.

<?php
    $birthDt = date("Y-m-d");
    echo date('Y-m-d', strtotime($birthDt. ' - 2 days'));
?>
 
// Result: 2023-01-04

Subtract 1 month from date.

<?php
    $birthDt = "2023-01-06";
    echo date('Y-m-d', strtotime($birthDt. ' - 1 month'));
?>
 
// Result: 2022-12-06

Subtract 1 year from date.

<?php
    $birthDt = "2023-01-06";
    echo date('Y-m-d', strtotime($birthDt. ' - 1 year'));
?>
 
// Result: 2022-01-06

I hope you get an idea about calculate the time difference between two times in hours and minutes php.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.