0

I want to add X seconds to the current time


So it should be like this
current_time()+$x
and formatted as dd-mm-YYYY hh:mm:ss

is there any way to do this?

7
  • add to what? this question makes little sesne
    – user557846
    Apr 23, 2014 at 21:46
  • please don't give negative points if you don't like please leave it, as if it marked negative more times, my account will be blocked. So Please
    – Mubin
    Apr 23, 2014 at 21:47
  • I can vote as i please.
    – user557846
    Apr 23, 2014 at 21:49
  • adding number of seconds to current time. like add 180 seconds to current time and make it look like 2:52 AM
    – Mubin
    Apr 23, 2014 at 21:49
  • Check this question: stackoverflow.com/questions/985818/…
    – BlueCacti
    Apr 23, 2014 at 21:54

2 Answers 2

2

i'd go with strtotime as it's easy to follow.

$timestamp = strtotime('+5 sec');
// date('d-m-Y H:i:s', $timestamp);
1

Better (and shorter) then mine, method (suggested by @Dan):

$currentDate = date('d-m-Y H:i:s', (time()));
$shiftedDate = date('d-m-Y H:i:s', (time()+5922725));

My answer:

You need mktime() function and date() function. You have to synergize this functions to achieve what you want.

Examples:

$currentTime = mktime(date("H"), date("i"), date("s"), date("m")  , date("d")+1, date("Y"));
$shiftedTime = mktime(date("H"), date("i"), date("s"),+5922725, date("m")  , date("d")+1, date("Y")); // with shift

Note, that mktime() function returns Unix Timestamp as result, so u have to convert timestamp to real date:

$currentTime = date('d-m-Y H:i:s', mktime(date("H"), date("i"), date("s"), date("m")  , date("d")+1, date("Y")));
// same for shifted

Sources:

mktime() on php.net
date() on php.net

4
  • 1
    Why not just use time()? Like this: date('d-m-Y H:i:s', (time()+5922725));
    – Dan
    Apr 23, 2014 at 21:49
  • Because I didn't know about it :) Know I do, I edited my answer. Apr 23, 2014 at 21:51
  • ahh... That's pretty cool :) but first method didn't worked for me and gives an error that some method has been deprecated. but the method provided by @Dan is perfectly working Thanks man.
    – Mubin
    Apr 23, 2014 at 21:58
  • Deprecated method or not depends on what version of php you use. Apr 23, 2014 at 22:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.