44

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;
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
user2536185
  • 463
  • 1
  • 4
  • 6

16 Answers16

58

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);
MrCode
  • 63,975
  • 10
  • 90
  • 112
54

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);
Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
41

You can use DateInterval

$date = new DateTime();
$interval = new DateInterval("PT15M");
$interval->invert = 1;
$date->add($interval);
echo $date->format("c") . "\n";
DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • 3
    This solution also will be better if you have timezone specified as part of the DateTime object. – dchayka Jun 19 '17 at 21:51
  • 3
    Much more readable creation of the `DateInterval` suggested [here](https://stackoverflow.com/a/6351113/878514): `$interval = DateInterval::createFromDateString('-15 minutes');` – fracz Apr 06 '18 at 23:07
25

you can try this as well,

$dateTimeMinutesAgo = new DateTime("15 minutes ago");
$dateTimeMinutesAgo = $dateTimeMinutesAgo->format("Y-m-d H:i:s");
Adil Abbasi
  • 3,161
  • 1
  • 40
  • 35
8

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.

eX0du5
  • 896
  • 7
  • 16
6
$currentTime = date('Y-m-d H:i:s');
$before15mins = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $before15mins);
zkanoca
  • 9,664
  • 9
  • 50
  • 94
6

You can also use strtotime function to subtract days, hours and/or seconds from current time.

echo date('Y-m-d H:i:s', strtotime('-15 minutes'));
Haritsinh Gohil
  • 5,818
  • 48
  • 50
Farhat Aziz
  • 131
  • 1
  • 10
4

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)));
Kshitiz
  • 2,673
  • 1
  • 18
  • 24
3

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');?>
Sintu Roy
  • 155
  • 9
2

Try using

$min = time() - 900; //900 seconds = 15 minutes 
zkanoca
  • 9,664
  • 9
  • 50
  • 94
Paul Wright
  • 445
  • 5
  • 16
2

To subtract 15 minutes you can do:

date('Y-m-d H:i:s', (time() - 60 * 15));

You can replace 15 with the number of minutes you want.

In case you're looking to subtract seconds you can simply do:

date('Y-m-d H:i:s', (time() - 10));

In this way you'll subtract 10 seconds.

Pascut
  • 3,291
  • 6
  • 36
  • 64
1

If you have only time value than below will be useful

// Your time
$time = '12:15:00';

// Returned value '12:00:00'
$newTime = date('H:i:s', strtotime($time) - (15*60));
1

To remove 5 minutes to a DateTime, i use :

$dateTime->modify("-5 minutes");
GuidN
  • 48
  • 4
0

I know this question is outdated but i just want to share how i did it in easy way

$current = new DateTime("10 minutes ago", new DateTimeZone('Asia/Manila') );

echo $current->format("Y-m-d H:i:s");
knhrvs
  • 3
  • 3
0

Working with DateTime is getting simpler in PHP. These are sample but you can try more

$dateTime->modify('-2 minutes');
$dateTime->modify('+2 minutes');
$dateTime->modify('+2 days');
$dateTime->modify('+2 years');
Solomon Tesfaye
  • 186
  • 2
  • 10
-1
//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;
Rahul Shukla
  • 7,365
  • 3
  • 15
  • 26
  • 1
    Is there any good reason to duplicate the existing answers? And why don't you use `DateTime` for this, providing a more readable code? – Nico Haase Aug 27 '19 at 10:30