avatar
step back one day by given time PHP

• Take advantage of the concept of "86400 milliseconds" which is equivalent to 24h to step back one day by a given timestamp.

» PHP

$timestamp = 1688745600; // Unix timestamp
$oneDayAgo = $timestamp - 86400;

echo date('Y-m-d', $oneDayAgo);

» Javascript

let timestamp = 1688745600; // Unix timestamp
let oneDayAgo = timestamp - 86400;

let date = new Date(oneDayAgo * 1000);
let formattedDate = date.toISOString().slice(0, 10);

console.log(formattedDate);

If the format is dd/mm/yyyy and here is an example:

let timestamp = 1688745600; // Unix timestamp
let oneDayAgo = timestamp - 86400;

let date = new Date(oneDayAgo * 1000);
let day = String(date.getDate()).padStart(2, '0');
let month = String(date.getMonth() + 1).padStart(2, '0');
let year = date.getFullYear();

let formattedDate = `${day}/${month}/${year}`;

» Java

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        long timestamp = 1688745600L; // Unix timestamp
        long oneDayAgo = timestamp - 86400;

        Instant instant = Instant.ofEpochSecond(oneDayAgo);
        LocalDate date = instant.atZone(ZoneId.systemDefault()).toLocalDate();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = date.format(formatter);
        System.out.println(formattedDate);
    }
}

» Python

import datetime

timestamp = 1688745600  # Unix timestamp
one_day_ago = timestamp - 86400

date = datetime.datetime.fromtimestamp(one_day_ago)
formatted_date = date.strftime("%Y-%m-%d")
print(formatted_date)
You need to login to do this manipulation!