{"id":2296,"date":"2021-07-13T04:34:06","date_gmt":"2021-07-13T04:34:06","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=2296"},"modified":"2025-04-07T12:38:17","modified_gmt":"2025-04-07T12:38:17","slug":"php-time","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-time\/","title":{"rendered":"PHP time"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to work with the PHP <code>time()<\/code> function to get the current timestamp in the local timezone.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-unix-timestamps'>Introduction to Unix timestamps <a href=\"#introduction-to-unix-timestamps\" class=\"anchor\" id=\"introduction-to-unix-timestamps\" title=\"Anchor for Introduction to Unix timestamps\">#<\/a><\/h2>\n\n\n\n<p>Computers store a date and time as a UNIX timestamp or a timestamp in short.<\/p>\n\n\n\n<p>A timestamp is an integer that refers to the number of seconds between 1970-01-01 00:00:00 UTC (Epoch) and the date and time to be stored.<\/p>\n\n\n\n<p>Computers store dates and times as timestamps because it is easier to manipulate an integer. For example, to add one day to a timestamp, it simply adds the number of seconds to the timestamp.<\/p>\n\n\n\n<p>PHP provides some helpful functions that manipulate timestamps effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-the-current-time'>Getting the current time <a href=\"#getting-the-current-time\" class=\"anchor\" id=\"getting-the-current-time\" title=\"Anchor for Getting the current time\">#<\/a><\/h2>\n\n\n\n<p>To get the current time, you use the <code>time()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">time<\/span><span class=\"hljs-params\">()<\/span>: <span class=\"hljs-title\">int<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>time()<\/code> function returns the current UNIX timestamp since Epoch (January 1 1970 00:00:00 GMT). For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">echo<\/span> time(); <span class=\"hljs-comment\">\/\/ 1626752728<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmVjaG8gdGltZSgpOyAvLyAxNjI2NzUyNzI4\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The return value is a big integer that represents the number of seconds since Epoch. To make the time human-readable, you use the <code>date()<\/code> function. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$current_time = time();\n<span class=\"hljs-keyword\">echo<\/span> date(<span class=\"hljs-string\">'Y-m-d g:ia'<\/span>, $current_time) . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRjdXJyZW50X3RpbWUgPSB0aW1lKCk7CmVjaG8gZGF0ZSgnWS1tLWQgZzppYScsICRjdXJyZW50X3RpbWUpIC4gJzxicj4nOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-number\">2021<\/span><span class=\"hljs-number\">-07<\/span><span class=\"hljs-number\">-13<\/span> <span class=\"hljs-number\">5<\/span>:<span class=\"hljs-number\">47<\/span>am<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>date()<\/code> function has two parameters.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first parameter specifies the date and time format. Here&#8217;s a <a href=\"https:\/\/www.php.net\/manual\/en\/datetime.format.php\" target=\"_blank\" rel=\"noreferrer noopener\">complete list of valid date and time formats<\/a>. <\/li>\n\n\n\n<li>The second parameter is an integer that specifies the timestamp.<\/li>\n<\/ul>\n\n\n\n<p>Since the <code>time()<\/code> function returns a timestamp, you can add seconds to it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='adding-to-subtracting-from-a-timestamp'>Adding to \/ subtracting from a timestamp <a href=\"#adding-to-subtracting-from-a-timestamp\" class=\"anchor\" id=\"adding-to-subtracting-from-a-timestamp\" title=\"Anchor for Adding to \/ subtracting from a timestamp\">#<\/a><\/h2>\n\n\n\n<p>The following example shows how to add a week to the current time:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$current_time = time();\n<span class=\"hljs-comment\">\/\/ 7 days later<\/span>\n$one_week_later =  $current_time + <span class=\"hljs-number\">7<\/span> * <span class=\"hljs-number\">24<\/span> * <span class=\"hljs-number\">60<\/span> * <span class=\"hljs-number\">60<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span> date(<span class=\"hljs-string\">'Y-m-d g:ia'<\/span>,$one_week_later);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRjdXJyZW50X3RpbWUgPSB0aW1lKCk7Ci8vIDcgZGF5cyBsYXRlcgokb25lX3dlZWtfbGF0ZXIgPSAgJGN1cnJlbnRfdGltZSArIDcgKiAyNCAqIDYwICogNjA7CgplY2hvIGRhdGUoJ1ktbS1kIGc6aWEnLCRvbmVfd2Vla19sYXRlcik7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this example, we add 7 days * 24 hours * 60 minutes * 60 seconds to the current time.<\/p>\n\n\n\n<p>Also, you can represent a time in the past by subtracting a number of seconds from the current time. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n$current_time = time();\n <span class=\"hljs-comment\">\/\/ 1 day ago<\/span>\n$yesterday = $current_time -  <span class=\"hljs-number\">24<\/span> * <span class=\"hljs-number\">60<\/span> * <span class=\"hljs-number\">60<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span> date(<span class=\"hljs-string\">'Y-m-d g:ia'<\/span>,$yesterday);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKJGN1cnJlbnRfdGltZSA9IHRpbWUoKTsKIC8vIDEgZGF5IGFnbwokeWVzdGVyZGF5ID0gJGN1cnJlbnRfdGltZSAtICAyNCAqIDYwICogNjA7CgplY2hvIGRhdGUoJ1ktbS1kIGc6aWEnLCR5ZXN0ZXJkYXkpOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='timezone'>timezone <a href=\"#timezone\" class=\"anchor\" id=\"timezone\" title=\"Anchor for timezone\">#<\/a><\/h2>\n\n\n\n<p>By default, the <code>time()<\/code> function returns the current time in the timezone specified in the PHP configuration file (php.ini).<\/p>\n\n\n\n<p>To get the current timezone, you can use the <code>date_default_timezone_get()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">echo<\/span> date_default_timezone_get(); <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmVjaG8gZGF0ZV9kZWZhdWx0X3RpbWV6b25lX2dldCgpOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>To set a specific timezone, you use the <code>date_default_timezone_set()<\/code>. It&#8217;s recommended that you use the UTC timezone. <\/p>\n\n\n\n<p>The following shows how to use the <code>date_default_timezone_set()<\/code> function to set the current timezone to the UTC timezone:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\ndate_default_timezone_set(<span class=\"hljs-string\">'UTC'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmRhdGVfZGVmYXVsdF90aW1lem9uZV9zZXQoJ1VUQycpOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='making-a-unix-timestamp'>Making a Unix timestamp <a href=\"#making-a-unix-timestamp\" class=\"anchor\" id=\"making-a-unix-timestamp\" title=\"Anchor for Making a Unix timestamp\">#<\/a><\/h2>\n\n\n\n<p>To make a Unix timestamp, you use the <code>mktime()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">mktime(\n    int $hour,\n    int|<span class=\"hljs-keyword\">null<\/span> $minute = <span class=\"hljs-keyword\">null<\/span>,\n    int|<span class=\"hljs-keyword\">null<\/span> $second = <span class=\"hljs-keyword\">null<\/span>,\n    int|<span class=\"hljs-keyword\">null<\/span> $month = <span class=\"hljs-keyword\">null<\/span>,\n    int|<span class=\"hljs-keyword\">null<\/span> $day = <span class=\"hljs-keyword\">null<\/span>,\n    int|<span class=\"hljs-keyword\">null<\/span> $year = <span class=\"hljs-keyword\">null<\/span>\n): int|<span class=\"hljs-keyword\">false<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>mktime()<\/code> function returns a Unix timestamp based on its arguments. If you omit an argument, <code>mktime()<\/code> function will use the current value according to the local date and time instead.<\/p>\n\n\n\n<p>The following example shows how to use the <code>mktime()<\/code> function to show that July 13, 2020, is on a Tuesday:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'July 13, 2021 is on a '<\/span> . date(<span class=\"hljs-string\">'l'<\/span>, mktime(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">13<\/span>, <span class=\"hljs-number\">2021<\/span>));<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmVjaG8gJ0p1bHkgMTMsIDIwMjEgaXMgb24gYSAnIC4gZGF0ZSgnbCcsIG1rdGltZSgwLCAwLCAwLCA3LCAxMywgMjAyMSkpOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">July 13, 2021 is on a Tuesday<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>time()<\/code> function to return the current timestamp since Epoch in local timezone.<\/li>\n\n\n\n<li>Use the <code>date_default_timezone_set()<\/code> function to set a specific timezone.<\/li>\n\n\n\n<li>Use the <code>date()<\/code> function to format the timestamp.<\/li>\n\n\n\n<li>Use <code>mktime()<\/code> function to create a timestasmp based on the year, month, day, hour, minute, and second.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2296\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-time\/\"\n\t\t\t\tdata-post-title=\"PHP time\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2296\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-time\/\"\n\t\t\t\tdata-post-title=\"PHP time\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to work with the PHP time() function to get the current timestamp in the local timezone.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":162,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2296","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=2296"}],"version-history":[{"count":4,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2296\/revisions"}],"predecessor-version":[{"id":3244,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2296\/revisions\/3244"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=2296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}