user2536185
user2536185

Reputation: 483

How to Subtract Minutes

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;

Upvotes: 46

Views: 94000

Answers (16)

Solomon Tesfaye
Solomon Tesfaye

Reputation: 346

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');

Upvotes: 0

GuidN
GuidN

Reputation: 108

To remove 5 minutes to a DateTime, i use :

$dateTime->modify("-5 minutes");

Upvotes: 4

Mehdi .H Jaff
Mehdi .H Jaff

Reputation: 21

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));

Upvotes: 1

knhrvs
knhrvs

Reputation: 13

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");

Upvotes: 0

Farhat Aziz
Farhat Aziz

Reputation: 146

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'));

Upvotes: 6

Pascut
Pascut

Reputation: 3434

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.

Upvotes: 3

Rahul Shukla
Rahul Shukla

Reputation: 8065

//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;

Upvotes: -1

Sintu Roy
Sintu Roy

Reputation: 155

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');?>

Upvotes: 3

Adil Abbasi
Adil Abbasi

Reputation: 3291

you can try this as well,

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

Upvotes: 28

Kshitiz
Kshitiz

Reputation: 2743

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)));

Upvotes: 4

zkanoca
zkanoca

Reputation: 9918

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

Upvotes: 6

Paul Wright
Paul Wright

Reputation: 455

Try using

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

Upvotes: 2

DevZer0
DevZer0

Reputation: 13525

You can use DateInterval

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

Upvotes: 42

Thomas Clayson
Thomas Clayson

Reputation: 29925

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);

Upvotes: 55

MrCode
MrCode

Reputation: 64526

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);

Upvotes: 58

eX0du5
eX0du5

Reputation: 896

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.

Upvotes: 8

Related Questions