{"id":103,"date":"2019-09-16T16:24:47","date_gmt":"2019-09-16T16:24:47","guid":{"rendered":"https:\/\/tutorialio.com\/?p=103"},"modified":"2025-11-04T10:31:59","modified_gmt":"2025-11-04T10:31:59","slug":"get-the-current-date-and-time-in-php","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/get-the-current-date-and-time-in-php\/","title":{"rendered":"How Can I Get the Current Date and Time in PHP?"},"content":{"rendered":"<p>Let\u2019s say you are working on a project that requires you to get the current date and\/or time in PHP. In this tutorial, you will learn about different methods that can be used to get the current date or time with or without timezones.<\/p>\n<h2 id=\"using-the-date-function-in-php\">Get the Current Date Using the date() Function<\/h2>\n<p>The <a href=\"http:\/\/us3.php.net\/manual\/en\/function.date.php\"><code>date()<\/code> function in PHP<\/a> can be used to return the local date and time in a format of your choice.<\/p>\n<p>You can use a lot of characters to specify the <a href=\"http:\/\/us3.php.net\/manual\/en\/function.date.php\">format of the outputted date string<\/a>. If you are only interested in the current date and time, knowing about the following date formatting characters should be sufficient.<\/p>\n<ul>\n<li><code>H<\/code> \u2014 Get the hour in the 24-hour format with leading zeroes (00 to 23).<\/li>\n<li><code>h<\/code> \u2014 Get the hour in the 12-hour format with leading zeroes (01 to 12).<\/li>\n<li><code>G<\/code> \u2014 Get the hour in the 24-hour format without leading zeroes (0 to 23).<\/li>\n<li><code>g<\/code> \u2014 Get the hour in the 12-hour format without leading zeroes  (1 to 12).<\/li>\n<li><code>i<\/code> \u2014 Get the minutes with leading zeroes (00 to 59).<\/li>\n<li><code>s<\/code> \u2014 Get the seconds with leading zeroes (00 to 59). <\/li>\n<li><code>m<\/code> \u2014 Get the numeric representation of a month with leading zeroes (01 to 12).<\/li>\n<li><code>M<\/code> \u2014 Get the short textual representation of a month with three letters (Jan to Dec).<\/li>\n<li><code>y<\/code> \u2014 Get two digit representation of a year (like 94 or 17).<\/li>\n<li><code>Y<\/code> \u2014 Get four digit representation of a year (like 1994 or 2017).<\/li>\n<li><code>d<\/code> \u2014 Get the day of the month as 2 digits with leading zeroes (01 to 31).<\/li>\n<li><code>D<\/code> \u2014 Get the short textual representation of a day with three letters (Mon to Sun).<\/li>\n<\/ul>\n<p>There are many more but these are the ones that you need to know in order to get the current date and time using the date() function.<\/p>\n<p>Here are a few examples that get the current date or time in PHP using the date() function:<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">\/\/ October 2, 2017, 9:11 PM\n$today = date(\"F j, Y, g:i A\");\n\n\/\/ 10.02.17\n$today = date(\"m.d.y\");\n\n\/\/ 09-11-51, 2-10-17, 1131 1151 Monday.\n$today = date('h-i-s, j-m-y, it is l.');\n\n\/\/ It is the 2nd day of the month.\n$today = date('\\I\\t \\i\\s \\t\\h\\e jS \\d\\a\\y \\o\\f \\t\\h\\e \\m\\o\\n\\t\\h.');\n\n\/\/ Date: 2, Month: 10, Year: 2017\n$today = date(\"\\D\\a\\\\t\\\\e: j, \\M\\o\\\\n\\\\t\\h: n, \\Y\\\\e\\a\\\\r: Y\");\n\n\/\/ 21:11:51\n$today = date(\"H:i:s\");\n\n\/\/ 2017-10-02 21:11:51\n$today = date(\"Y-m-d H:i:s\");\n<\/code><\/pre>\n<p>In the above example, you can see that I had to escape all the characters in order to display them literally. For instance, <code>it is l.<\/code> became <code>1131 1151 Monday.<\/code> without escaping the characters. This is because <code>i<\/code> gave the number of minutes with leading zeroes, <code>s<\/code> gave the number of seconds with leading zeroes and <code>t<\/code> gave the number of days in the month (31 for October in our case).<\/p>\n<p>With double quotes, we had to use two backslashes for characters like <code>t<\/code> and <code>n<\/code> in order to prevent them from rendering tabs and new lines.<\/p>\n<p>You can also get the current date and time for a particular timezone with the help of the <code>date_default_timezone_set()<\/code> function. Here are a few examples that show the time in different parts of the world.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$timezone = date_default_timezone_get();\n\n\/\/ Output \u2014 The current timezone is: Europe\/Berlin\necho \"The current timezone is: \".$timezone;\n\/\/ Output \u2014 Current date and time in Berlin is: 02 Oct, 17 10:22:43 PM\necho \"Current date and time in Berlin is: \".date('d M, y h:i:s A');\n\ndate_default_timezone_set('America\/Denver');\n\/\/ Output \u2014 Current date and time Denver is: 02 Oct, 17 02:22:43 PM\necho \"Current date and time Denver is: \".date('d M, y h:i:s A').\"\\n\";\n\ndate_default_timezone_set('Australia\/Sydney');\n\/\/ Output \u2014 Current date and time Sydney is: 03 Oct, 17 07:22:43 AM\necho \"Current date and time Sydney is: \".date('d M, y h:i:s A').\"\\n\";\n\ndate_default_timezone_set('Asia\/Kolkata');\n\/\/ Output \u2014 Current date and time Kolkata is: 03 Oct, 17 01:52:43 AM\necho \"Current date and time Kolkata is: \".date('d M, y h:i:s A').\"\\n\";\n<\/code><\/pre>\n<h2 id=\"using-serverrequesttime\">Get the Timestamp Using $_SERVER[&#8216;REQUEST_TIME&#8217;]<\/h2>\n<p>In PHP, <code>$_SERVER<\/code> is an array that contains information like headers, paths or script locations. One of the indices of this array is <code>REQUEST_TIME<\/code>. It returns the timestamp at the start of a request. This index has been available since PHP 5.1.0.<\/p>\n<p>Similarly, you can also use the <code>REQUEST_TIME_FLOAT<\/code> index. This will also give you the timestamp of the start of a request. However, it will have a microsecond precision and it has been available since PHP 5.4.0.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">$timestamp_a = $_SERVER['REQUEST_TIME'];\n$timestamp_b = $_SERVER['REQUEST_TIME_FLOAT'];\n\n\/\/ Output \u2014 1506973854\necho $timestamp_a;\n\n\/\/ Output \u2014 1506973854.3914\necho $timestamp_b;\n<\/code><\/pre>\n<h2 id=\"using-the-time-function-in-php\">Get the Timestamp Using time() Function<\/h2>\n<p>Just like <code>$_SERVER['REQUEST_TIME']<\/code>, you can also get the current UNIX timestamp using the <a href=\"http:\/\/php.net\/manual\/en\/function.time.php\">time() function in PHP<\/a>. It will return the current time measured in number of seconds since Unix epoch (January 1 1970 00:00:00 GMT).<\/p>\n<p>One important difference between <code>time()<\/code> and <code>$_SERVER['REQUEST_TIME']<\/code> is that the latter will return the timestamp at the start of the request. This may or may not be significant depending on the project.<\/p>\n<p class=\"lang-name\">PHP<\/p>\n<pre><code class=\"language-php\">sleep(1);\n$timestamp_a = time();\n$timestamp_b = $_SERVER['REQUEST_TIME'];\n\n\/\/ Output \u2014 1506974986\necho $timestamp_a.\"\\n\";\n\n\/\/ Output \u2014 1506974985\necho $timestamp_b.\"\\n\";\n<\/code><\/pre>\n<p>As you can see, with the addition of <code>sleep()<\/code> function to delay the execution of the program by 1 second, the timestamp values for <code>$_SERVER['REQUEST_TIME']<\/code> and <code>time()<\/code> changed by one second. This is because <code>time()<\/code> get the current time stamp value while <code>$_SERVER['REUQEST_TIME']<\/code> used the timestamp value it obtained at the start of the request.<\/p>\n<p>One more important thing which I would like is the time timestamp value returned by either <code>$_SERVER['REUQEST_TIME']<\/code> or <code>time()<\/code> is independent of the time zone. It is based on the UTC value.<\/p>\n<h2 id=\"quick-summary\">Quick Summary<\/h2>\n<p>Let\u2019s recap everything that we have covered in this tutorial.<\/p>\n<ol id=\"summary-points\">\n<li>You can get the current date and time using the <code>date()<\/code> function in PHP. This function accepts different characters to output the date and time in different formats.<\/li>\n<li>If you want the current time as a timestamp, you can use the <code>REQUEST_TIME<\/code> index of the <code>$_SERVER<\/code> superglobal array. Timstamp with microsecond precision can be obtained using the <code>REQUEST_TIME_FLOAT<\/code> index.<\/li>\n<li>The timestamp returned by <code>REQUEST_TIME<\/code> is obtained at the start of the request. If you want the timestamp value of a later time, you can use the <code>time()<\/code> function in PHP.<\/li>\n<\/ol>\n<p>Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to get the current date and time in PHP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s say you are working on a project that requires you to get the current date and\/or time in PHP. In this tutorial, you will learn about different methods that can be used to get the current date or time with or without timezones. Get the Current Date Using the date() Function The date() function [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-103","post","type-post","status-publish","format-standard","hentry","category-how-to-guides"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=103"}],"version-history":[{"count":1,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/103\/revisions"}],"predecessor-version":[{"id":10246,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/103\/revisions\/10246"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}