6 Replies - 3919 Views - Last Post: 23 September 2012 - 04:13 AM Rate Topic: -----

#1 SimonSays  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 06-March 10

Subtracting 5 minutes from Date/Time

Posted 22 September 2012 - 02:27 PM

Hello All,
I am trying to find a good way to get the current time/date, and then get that time 5 minutes delayed. This is what I am doing to get the current date/time.
$format = "YmjHi";
// Change timezone to GMT.
date_default_timezone_set("GMT");
$gmttime = date($format);



I am new to PHP, but I was hoping there is an easy way for me to subtract a given amount of time from the date/time.

Thanks!

This post has been edited by SimonSays: 22 September 2012 - 02:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Subtracting 5 minutes from Date/Time

#2 BenignDesign  Icon User is online

  • holy shitin shishkebobs
  • member icon




Reputation: 6086
  • View blog
  • Posts: 10,526
  • Joined: 28-September 07

Re: Subtracting 5 minutes from Date/Time

Posted 22 September 2012 - 02:57 PM

Your best bet will probably be to separate your individual portions (ie, month, day, year, hour, minutes) as each of these variables has the potential to change depending upon your subtraction.

If the current date/time is January 1, 2013, 00:04, and you subtract 5 minutes, your date/time becomes December 31, 2012, 23:59. Each individual variable needs to roll back by one.

I could be wrong, but I would say you would need to set up if statements to determine if the change in minutes is going to change any (or all) of the other variables in the string. I don't think you're going to find an "easy" solution to the problem.
Was This Post Helpful? 0
  • +
  • -

#3 SimonSays  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 06-March 10

Re: Subtracting 5 minutes from Date/Time

Posted 22 September 2012 - 03:18 PM

That is what I was afraid of! I had starting doing it that way, so I guess that is how I will continue unless someone can point me too a better way.
Thanks for your response.
Was This Post Helpful? 0
  • +
  • -

#4 nightscream  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 237
  • Joined: 04-December 08

Re: Subtracting 5 minutes from Date/Time

Posted 23 September 2012 - 03:06 AM

There are 2 methods I'll be explaining'

Method #1
You can use the datetime object and his sub function check the php manual for it.
This one is more OO because of the use of objects

Method #2
Default time zone set..
$date = date($format, time() - 60 * 5);

This will substract 5 minutes ( 60 sec * 5)
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Subtracting 5 minutes from Date/Time

Posted 23 September 2012 - 03:47 AM

Or use strtotime:

<?php

$format = "YmjHi";
date_default_timezone_set('GMT');
echo date($format, strtotime("-5 minute")) . "\n";

Was This Post Helpful? 1
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Subtracting 5 minutes from Date/Time

Posted 23 September 2012 - 04:01 AM

View PostJackOfAllTrades, on 23 September 2012 - 11:47 AM, said:

<?php
$format = "YmjHi";
date_default_timezone_set('GMT');
echo date($format, strtotime("-5 minute")) . "\n";


This.

Or you can use the newer DateTime classes:

<?php
$format = "YmjHi";
$sub = new DateInterval("PT5M"); // Interval of 5 mins
$date = new DateTime(); // Current time
$date->sub($sub); // Subtract
echo $date->format($format);

Was This Post Helpful? 2
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Subtracting 5 minutes from Date/Time

Posted 23 September 2012 - 04:13 AM

PHP: Providing 8,000 different ways to accomplish every task :)
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1