0

Could someone assist me on a timestamp issue.. how can I subtract 2 minutes from this timestamp?

    echo 'Settings from database (octopus_import_employees):';
    $settings = get_settings('octopus_import_employees');
    var_dump($settings);

    echo 'Timestamp in human format (Started timestamp):';
    $started = date("Y-m-d H:i:s", $settings['started']);
    var_dump($started);

var_dump($settings); gets an unix timestamp such as: 342534534

var_dump($started); converts it to a readable format such as: 2019-11-08 05:08:58.

All help would be appreciated.

Sema
  • 95
  • 9

2 Answers2

3

Timestamps are in seconds so you can subtract 120 seconds (i.e. 2 minutes) from it

$time = $settings['started'] - 120;

echo 'Timestamp in human format (Started timestamp):';
$started = date("Y-m-d H:i:s", $time);
var_dump($started);
Yasin Patel
  • 5,624
  • 8
  • 31
  • 53
2

@YasinPatel solution is definitely the simplest for your situation. In situations where you don't have a unix timestamp input, one of these methods might be easier to use.

You could create a DateTime object from your timestamp using date_create_from_format and subtract 2 minutes from it, using either sub or modify:

$started = date_create_from_format('U', $settings['started']);
$started->sub(new DateInterval('PT2M'));
echo $started->format('Y-m-d H:i:s');

or

$started = date_create_from_format('U', $settings['started']);
$started->modify('-2 minutes');
echo $started->format('Y-m-d H:i:s');

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95