Hello,
I would like to turn back the time 10 seconds before current time and output in this format date('H/i/s')
How can I do this ?
I dont want to get current time then remove 10 seconds from time .
Thanks
Hello,
I would like to turn back the time 10 seconds before current time and output in this format date('H/i/s')
How can I do this ?
I dont want to get current time then remove 10 seconds from time .
Thanks
To change system time, you need to call the OS' API to change the time. On Linux, you can do this with the date
command:
date MMDDhhmmYYYY.ss
For example, date 092619222010.30
will set the date and time to Sep 26 2010 19:22:30
. On PHP you could try to use the system()
1 function to do that.
$stamp = date('mdHiY.s', time()-10);
system("date $stamp");
1 PHP runs as the same user as Apache if it's running under mod_php (the default setup). This usually means you won't have permissions to do it. If that happens to be the case, you can get around it by calling another script or program that can change the time.
I personally try to avoid playing directly with the time()
value, for reasons when trying to add or subtract days/weeks/etc. with leap years, etc. For all practical purposes, strtotime()
is always reliable :
echo date('H/i/s', strtotime('-10 seconds', time())); // time() - 10 seconds
** EDIT **
Why time() - 10
is not good?
<?php
// this is my timezone, but it applies to any DST zones....
date_default_timezone_set('America/Montreal');
$time = strtotime('2010-11-07 02:00:05');
var_dump(date(DATE_RFC822, $time - 10));
var_dump(date(DATE_RFC822, strtotime('-10 seconds', $time)));
The output of this is
string(29) "Sun, 07 Nov 10 01:59:55 -0500"
string(29) "Sun, 07 Nov 10 01:59:55 -0400"
Note how the UTC changes in the second example? Where it is not entirely correct and should be Sun, 07 Nov 10 02:59:55 -0500
(see here to reset it back to the correct UTC) subtracting 10 to time()
is even worst! It returns a timestamp off by 1 hour in time. This could lead to potential data errors, etc.
The same thing applies adding and subtracting days and months; directly manipulating time()
is simply not a good idea and strtotime()
will always return the correct timestamp for the given time manipulation. Period.