How to get date and time from server

后端 未结 7 1786
感动是毒
感动是毒 2020-12-14 16:48

I want to retrieve date and time from server and according to it do some thing. For this I used following code:

$info = getdate();
$date = $info[\'mday\'];
$         


        
相关标签:
7条回答
  • 2020-12-14 16:54

    Try this -

    <?php
    date_default_timezone_set('Asia/Kolkata');
    
    $timestamp = time();
    $date_time = date("d-m-Y (D) H:i:s", $timestamp);
    echo "Current date and local time on this server is $date_time";
    ?>
    
    0 讨论(0)
  • 2020-12-14 17:10

    You can use the "system( string $command[, int &$return_var] ) : string" function. For Windows system( "time" );, system( "time > output.txt" ); to put the response in the output.txt file. If you plan on deploying your website on someone else's server, then you may not want to hardcode the server time, as the server may move to an unknown timezone. Then it may be best to set the default time zone in the php.ini file. I use Hostinger and they allow you to configure that php.ini value.

    0 讨论(0)
  • 2020-12-14 17:13

    You should set the timezone to the one of the timezones you want. let set the Indian timezone

    // set default timezone
    date_default_timezone_set('Asia/Kolkata');
    
    $info = getdate();
    $date = $info['mday'];
    $month = $info['mon'];
    $year = $info['year'];
    $hour = $info['hours'];
    $min = $info['minutes'];
    $sec = $info['seconds'];
    
    $current_date = "$date/$month/$year == $hour:$min:$sec";
    
    0 讨论(0)
  • 2020-12-14 17:14

    No need to use date_default_timezone_set for the whole script, just specify the timezone you want with a DateTime object:

    $now = new DateTime(null, new DateTimeZone('America/New_York'));
    $now->setTimezone(new DateTimeZone('Europe/London'));    // Another way
    echo $now->format("Y-m-d\TH:i:sO"); // something like "2015-02-11T06:16:47+0100" (ISO 8601)
    
    0 讨论(0)
  • 2020-12-14 17:15

    For enable PHP Extension intl , follow the Steps..

    1. Open the xampp/php/php.ini file in any editor.
    2. Search ";extension=php_intl.dll"
    3. kindly remove the starting semicolon ( ; ) Like : ;extension=php_intl.dll. to. extension=php_intl.dll.
    4. Save the xampp/php/php.ini file.
    5. Restart your xampp/wamp.
    0 讨论(0)
  • 2020-12-14 17:16

    You should set the timezone to the one of the timezones you want.

    // set default timezone
    date_default_timezone_set('America/Chicago'); // CDT
    
    $info = getdate();
    $date = $info['mday'];
    $month = $info['mon'];
    $year = $info['year'];
    $hour = $info['hours'];
    $min = $info['minutes'];
    $sec = $info['seconds'];
    
    $current_date = "$date/$month/$year == $hour:$min:$sec";
    

    Or a much shorter version:

    // set default timezone
    date_default_timezone_set('America/Chicago'); // CDT
    
    $current_date = date('d/m/Y == H:i:s');
    
    0 讨论(0)
提交回复
热议问题