Sunteți pe pagina 1din 3

Buat xml sitemap dengan php | mysqli tutorial php | jquery ui menubutton | autoc omplete search in ajax pontikis.

net Tech blog & Web labs Home Blog Tips Labs About Subscribe to our Newsletter

RSS feed search How to Get Current Datetime (NOW) with PHP August 30th, 2013 Share: 30,734

To get current datetime (now) with PHP you can use date with any PHP version, or better datetime class with PHP >= 5.2 Various date format expressions are available here. Example using date This expression will return NOW in format Y-m-d H:i:s 1 2 3 <?php echo date('Y-m-d H:i:s'); ?> Example using datetime class This expression will return NOW in format Y-m-d H:i:s 1 2 3 4 <?php $dt = new DateTime(); echo $dt->format('Y-m-d H:i:s'); ?> A more complete approach Above examples will return NOW using your server timezone, as it is defined in p hp.ini, for example: 1 2 3 4

[Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone = Europe/Athens But, the best approach is to save dates to UTC. UTC (also called Zulu time) is t he standard international time. All time zones are expressed as offsets of UTC. Especially, if your application users are located in different timezones. Using php DateTime class it is easy to compute the time in user timezone from UTC, and you don't have to worry about for Daylight Savings Time changes. Define server timezone and server date format according to your preferences. My preferences are: 1 2 3 4 5 /* server timezone */ define('CONST_SERVER_TIMEZONE', 'UTC'); /* server dateformat */ define('CONST_SERVER_DATEFORMAT', 'YmdHis'); In this case, you may use the following simple function: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <?php /** * Converts current time for given timezone (considering DST) * to 14-digit UTC timestamp (YYYYMMDDHHMMSS) * * DateTime requires PHP >= 5.2

* * @param $str_user_timezone * @param string $str_server_timezone * @param string $str_server_dateformat * @return string */ function now($str_user_timezone, $str_server_timezone = CONST_SERVER_TIMEZONE, $str_server_dateformat = CONST_SERVER_DATEFORMAT) { // set timezone to user timezone date_default_timezone_set($str_user_timezone); $date = new DateTime('now'); $date->setTimezone(new DateTimeZone($str_server_timezone)); $str_server_now = $date->format($str_server_dateformat); // return timezone to server default date_default_timezone_set($str_server_timezone); return $str_server_now; } ?> Hope to find it helpful. Your comments are welcomed! Related Posts You may also be interested in

Easily Handling Dates with PHP5 in Any Database ?! Sign-up for our free email newsletter. Get updates when new tutorials and tips hed. You can unsubscribe anytime with a click. About the author

Your comments are welcomed! This site actively encourages commenting on any post. Comments are not pre-moder ated, but this community does not tolerate direct or indirect attacks, name-call ing or insults. Please, read terms of use and Comment Policy at privacy policy. More tips Creative Commons License Terms Privacy Contact us Home Blog Tips Labs About

S-ar putea să vă placă și