Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to find unix timestamp in PHP?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 327
    Comment on it

    Hi Reader's,
    Welcome to FindNerd, today we are going to discuss how to find Unix timestamp in PHP?

    If you want to get current Unix timestamp then you should use getTimestamp() function which returns Unix timestamp that represents the date. This function has no parameter.

    getTimestamp() function never return the UTC timestamp. This function returns the timestamp relative to the set timezone, or the default server timezone.

    Note:-getTimestamp() function always represent unix timestamp of current time on your system or server.

    Why use Unix Timestamp:

    If we are making a web application in PHP, then we need to set multiple timezones according to our region. It calculates time in a second point according to time zone. In simple word we can say that the Unix timestamp describes the same time according to a region. It count starts at the Unix Epoch on January 1st, 1970 at UTC.

    You can see below example:-

    <?php
      //create the object
      $datetime = new DateTime();
      //get unix timestamp
      echo $datetime->getTimestamp();
    
    ?>

    Output will come following:

    1466426894 

    This output is Unix timestamp.

    IF you want to change this unix timestamp  in date/time then you can see below code:

    <?php
      //get the object
      $datetime = new DateTime();
      //get unix timestamp
      echo $datetime->getTimestamp();
      echo '</br>';
      $date = date("l jS of  Y h:i:s A",  $getTimestamp);
      echo $date;
    
    ?>

    Output will come following:

    1466764248
    Thursday 1st 1970f January 1970 05:30:00 AM 

    here another example of date_timestamp_set()

    <?php
       //create a object
        $Getdate = date_create();
        echo date_format($Getdate, 'U = Y-m-d H:i:s') . "\n";
        //get the timestamp
        date_timestamp_set($Getdate, 1367275022);
        echo '</br>';
        echo date_format($Getdate, 'U = Y-m-d H:i:s') . "\n";
    
    ?>

    Output will come following:

    1467276124 = 2016-06-30 14:12:04
    1367275022 = 2013-04-30 04:07:02 

    I hope this blog is helpful for you to get current unix timestamp in PHP.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: