PHP : Get timestamp of any previous days

How to get the timestamp of a certain moment? For example 10 hours from now. Or to get the timestamp 5 days back. I found a way to do this using date() and time() function of PHP. Here is the example to find the timestamp.

  • Find the timestamp of 5 hours from now

  • $timestamp = date('y-m-d h:i:s', time() - 5*60*60);
    print "$timestamp\n";


  • Find the timestamp of 1 hours from now

  • $timestamp = date('y-m-d h:i:s', time() - 1*60*60);
    print "$timestamp\n";


  • Find the timestamp of 2 days from now

  • $timestamp = date('y-m-d h:i:s', time() - 2*24*60*60);
    print "$timestamp\n";


  • Find the timestamp of 100 days from now

  • $timestamp = date('y-m-d h:i:s', time() - 100*24*60*60);
    print "$timestamp\n";


    I think this four examples are pretty enough for you to understand the tricks. Just change the timestamp difference, that's it. For example to find the timestamp of N day from now will be N*24*60*60

    Comments

    Ashwin said…
    Thanks a lot. Your simple piece of code helped me a lot in time

    Regards,
    Ashwin