Home » Php » php – How to Subtract Minutes

php – How to Subtract Minutes

Posted by: admin April 23, 2020 Leave a comment

Questions:

I want to send a reminder email.I don’t want to use cron on Linux/Unix/BSD box or Scheduled Tasks on Windows.

I’m trying to subtract 15 minutes from the current time.

here is my code so far (doesn’t work):

$days   = date("j",time());
$months = date("n",time());
$years  = date("Y",time());
$hours  = date("G",time());
$mins   = (date("i",time()));
$secs   = date("s",time());
$mins   = $mins-15;
How to&Answers:

Change the date into a timestamp (in seconds) then minus 15 minutes (in seconds) and then convert back to a date:

$date = date("Y-m-d H:i:s");
$time = strtotime($date);
$time = $time - (15 * 60);
$date = date("Y-m-d H:i:s", $time);

Answer:

To subtract 15 minutes from the current time, you can use strtotime():

$newTime = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $newTime);

Answer:

You can use DateInterval

$date = new DateTime();
$interval = new DateInterval("PT15M");
$interval->invert = 1;
$date->add($interval);
echo $date->format("c") . "\n";

Answer:

you can try this as well,

$dateTimeMinutesAgo = new DateTime("15 minutes ago");
$dateTimeMinutesAgo = $dateTimeMinutesAgo->format("Y-m-d H:i:s");

Answer:

How about substracting the 15 minutes from time() before converting it?

$time = time() - (15 * 60);

And then use $time instead of time() in your code.

Answer:

$currentTime = date('Y-m-d H:i:s');
$before15mins = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $before15mins);

Answer:

Following is the way you can add days / hours / minutes / sec to current time

  $addInterval = date('Y-m-d H:i:s', strtotime("+$days days $hours hours $minute minute $sec second", strtotime(currentTime)));

Answer:

Try using

$min = time() - 900; //900 seconds = 15 minutes 

Answer:

You can also use DateInterval object

<?php
    $date = new DateTime('Y-m-d H:i:s');
    $date->sub(new DateInterval('PT10H30S'));
    echo $date->format('Y-m-d H:i:s');?>

Answer:

//To Get Current DateTime
$currentDate = date("Y-m-d H:i:s");

//To Get Current DateTime - 15Min
$oldDate = date("Y-m-d H:i:s", strtotime($currentDate) - (15 * 60));

echo $currentDate;
echo $oldDate;