tags:

views:

112

answers:

3
+2  Q: 

PHP Turn back time

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

+6  A: 
echo date('H/i/s', time()-10);
Jacob Relkin
This answer is no longer valid
NullUserException
@NullUserException, Why not?
Jacob Relkin
@Jacob I thought the user edited the question to mean they wanted to change the system time.
NullUserException
@NullUserException - Then why did they accept the answer? I am confused too; it seems like this answer is doing exactly what the user didn't want (subtracting from `time()`.... which does seem like the correct thing to do btw).... but they accepted it??
Peter Ajtai
@Peter Yeah, it beats me
NullUserException
+3  A: 

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.

NullUserException
This is not the answer he wanted. He was asking for a solution in PHP, which is usually running as the same user as the webserver does. Even with system() he will not be able to change the system time.
halfdan
@halfdan Well, what do you suggest?
NullUserException
See the accepted answer. He just wants to output the time form 10secs ago, not change the system time. So why not just substract 10seconds from time()?
halfdan
@halfdan That's what I assumed (and answered) initially. But the OP edited the question and added the following: "I dont want to get current time then remove 10 seconds from time ."
NullUserException
That's a little misleading, I agree. But his wording suggests that he isn't very experienced, so I concluded that he just wants to substract 10 seconds from the current time. (Didn't downvote you anyway, answer is still good)
halfdan
+5  A: 

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.

Yanick Rochon
"value, for reasons when trying to add or subtract days/weeks/etc. with leap years, etc" --- time() is just a number of seconds since unix epoch and has nothing to do with leap years etc. It grows monotonously regardless leap year or anything else.
zerkms
@zerkms, read the edit
Yanick Rochon
I know that, and now you get `echo $time - strtotime('-10 seconds', $time);`. But only 10 seconds left, not 3610. The issue is in the date formatting, not in its calculation. Time flows monotonously, and if 10 seconds left, then difference is 10. If no - fix your date() function.
zerkms
@zerkms, `echo $time - strtotime('-10 seconds', $time);` echo 3610 for me while `echo $time - ($time - 10);` echo 10... the latter is wrong (where $time is "Sun, 07 Nov 10 02:00:05" or course). The problem is not that it doesn't matter on most case, the problem is that it does on certain "unpredictable" ones.
Yanick Rochon
Timestamp is a number of second since 1 Jan 1970. It grows **monotonously** second by second. If 10 seconds left, then timestamps are differ by **10**. Point. You have an issue with **date formatting**. Your date function has no idea about summer and non-summer time - that is why you trying to cheat.
zerkms
@zerkms, you can justify however you want, but direct manipulation of time does not guarantee to return the correct timestamps all the time in every timezones. Time may increment *monotonously* as you say, but there is a time change in DST for a fact, and `time() - 10` during that change yields incorrect timestamps, whereas `strtotime('-10 seconds')` is **always** correct. You won't win here.
Yanick Rochon
**Each timestamp is correct**. Each timestamp is existing in each timezone. "and time() - 10 during that change yields incorrect timestamps" --- nope. Timestamp is correct, but the function that formats that timestamp into human readable format - is not. The weak chain here is `date()`, which cannot determine whether time is summer or winter, and `not time()`.
zerkms
@zerkms, this is exaclty what I'm saying. Direct manipulation `$time() - 10 = 1289113195` where `strtotime('-10 seconds', $time) = 1289109595` ... do you see here? The first one, how wrong it is? This has nothing to do with `date()` but with the calculation itself. I told you, you won't win here.
Yanick Rochon
upvoted, but i don't agree anyway.
zerkms