Php turn back time

I would like to return the time 10 seconds before the current time and print it on this date of the format ("H / i / s")

How can i do this?

I do not want to get the current time, and then delete 10 seconds from the time.

thank

+3
source share
3 answers
echo date('H/i/s', time()-10);
+7
source

I personally try to avoid directly reproducing the value time()for reasons when you are trying to add or subtract days / weeks / etc. with leap years, etc. For all practical purposes, it is strtotime()always reliable:

echo date('H/i/s', strtotime('-10 seconds', time())); // time() - 10 seconds

** EDIT **

Why is time() - 10 it bad ?

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

Result of this

string(29) "Sun, 07 Nov 10 01:59:55 -0500"
string(29) "Sun, 07 Nov 10 01:59:55 -0400"

, UTC ? Sun, 07 Nov 10 02:59:55 -0500 (. reset UTC), 10 time() ! 1 . ..

; time() - , strtotime() . .

+5

, API OS, . Linux date:

date MMDDhhmmYYYY.ss

, date 092619222010.30 Sep 26 2010 19:22:30. PHP system() 1 .

$stamp = date('mdHiY.s', time()-10);
system("date $stamp");

1 PHP works as the same user as Apache if it runs under mod_php (default setting). This usually means that you will not have rights to do so. If this happens, you can work around it by calling another script or program that can change the time.

+3
source

Source: https://habr.com/ru/post/1766676/


All Articles