d<\/code><\/td>Day of the month. two digits with leading zeros<\/td> | 01<\/code> – 31<\/code><\/td><\/tr>j<\/code><\/td>Day of the month without leading zeros<\/td> | 1<\/code> – 31<\/code><\/td><\/tr>S<\/code><\/td>Includes the English suffix.<\/td> | st<\/code>, nd<\/code>, rd<\/code>, th<\/code> (e.g. 1st<\/code>, 2nd<\/code>, 3rd<\/code>, 4th<\/code>)<\/td><\/tr>Weekday<\/strong><\/td><\/td> | <\/td><\/tr> | D<\/code><\/td>Abbreviated textual representation of a day, in three letters<\/td> | Sun<\/code> – Sat<\/code><\/td><\/tr>l<\/code><\/td>A full textual representation of a weekday.<\/td> | Sunday<\/code> – Saturday<\/code><\/td><\/tr>Month<\/strong><\/td><\/td> | <\/td><\/tr> | F<\/code><\/td>A full textual representation of a month, such as January or March<\/td> | January<\/code> – December<\/code><\/td><\/tr>M<\/code><\/td>Abbreviated textual representation of a month, in three letters<\/td> | Jan<\/code> – Dec<\/code><\/td><\/tr>m<\/code><\/td>Numeric representation of a month, with leading zeros<\/td> | 01<\/code> – 12<\/code><\/td><\/tr>n<\/code><\/td>Numeric representation of a month, without leading zeros<\/td> | 1<\/code> – 12<\/code><\/td><\/tr>Year<\/strong><\/td><\/td> | <\/td><\/tr> | Y<\/code><\/td>A full numeric representation of a year, 4 digits<\/td> | E.g. 1999<\/code> or 2003<\/code><\/td><\/tr>y<\/code><\/td>A two digit representation of a year<\/td> | E.g. 99<\/code> or 03<\/code><\/td><\/tr>Time<\/strong><\/td><\/td> | <\/td><\/tr> | A<\/code><\/td>Uppercase Ante Meridiem and Post Meridiem<\/td> | AM<\/code> or PM<\/code><\/td><\/tr>g<\/code><\/td>12-hour format of an hour without leading zeros<\/td> | 1<\/code> – 12<\/code><\/td><\/tr>h<\/code><\/td>12-hour format of an hour with leading zeros<\/td> | 01<\/code> – 12<\/code><\/td><\/tr>i<\/code><\/td>Minutes with leading zeros<\/td> | 00<\/code> – 59<\/code><\/td><\/tr>s<\/code><\/td>Seconds with leading zeros<\/td> | 00<\/code> – 59<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\nThe DateTime<\/code> object can be converted to a timestamp:<\/p>\n\n\n\n<?php\n\n$now = new DateTime();\necho $now->getTimestamp(); \/\/ 1634139081<\/code><\/pre>\n\n\n\nBut we can also get the current time in timestamp without constructing a DateTime<\/code> object:<\/p>\n\n\n\n<?php\n\necho time(); \/\/ 1634139081<\/code><\/pre>\n\n\nConstruct a DateTime<\/code> object of a specific time<\/h3>\n\n\nWhat if we want to construct a DateTime<\/code> for a particular time, like July 14th, 2011? We can pass a formatted string date to the constructor:<\/p>\n\n\n\n<?php\n\n$date = new DateTime(\"2011-07-14\");\nvar_dump($date);\n\n\/\/ object(DateTime)#1 (3) {\n\/\/ [\"date\"]=>\n\/\/ string(26) \"2011-07-14 00:00:00.000000\"\n\/\/ [\"timezone_type\"]=>\n\/\/ int(3)\n\/\/ [\"timezone\"]=>\n\/\/ string(12) \"Asia\/Jakarta\"\n\/\/ }<\/code><\/pre>\n\n\n\nThe constructor accepts other formats as well:<\/p>\n\n\n\n <?php\n\n$date = new DateTime(\"14-07-2011\");\nvar_dump($date);\n\n\/\/ object(DateTime)#1 (3) {\n\/\/ [\"date\"]=>\n\/\/ string(26) \"2011-07-14 00:00:00.000000\"\n\/\/ [\"timezone_type\"]=>\n\/\/ int(3)\n\/\/ [\"timezone\"]=>\n\/\/ string(12) \"Asia\/Jakarta\"\n\/\/ }<\/code><\/pre>\n\n\n\nBut be careful with an ambiguous format, like this:<\/p>\n\n\n\n <?php\n\n$date = new DateTime(\"07\/14\/2011\");\nvar_dump($date);\n\n\/\/ object(DateTime)#1 (3) {\n\/\/ [\"date\"]=>\n\/\/ string(26) \"2011-07-14 00:00:00.000000\"\n\/\/ [\"timezone_type\"]=>\n\/\/ int(3)\n\/\/ [\"timezone\"]=>\n\/\/ string(12) \"Asia\/Jakarta\"\n\/\/ }<\/code><\/pre>\n\n\n\nYou might think that everyone should be familiar with an American date format. But not everyone is and it might be interpreted differently. Not PostgreSQL.<\/p>\n\n\n\n CREATE TABLE IF NOT EXISTS public.datetime_demo\n(\n created_at date\n);\n\ninsert into datetime_demo (created_at) values ('07\/12\/2011');\n\nselect created_at from datetime_demo; \/* 2011-12-07 *\/<\/code><\/pre>\n\n\n\nYou may have thought that would return July 12th, 2011, but it was December 7th, 2011, instead. A better way is to use an explicit format:<\/p>\n\n\n\n <?php\n\n$date = DateTime::createFromFormat('m\/d\/y', \"10\/08\/21\");\nvar_dump($date);\n\n\/\/object(DateTime)#2 (3) {\n\/\/ [\"date\"]=>\n\/\/ string(26) \"2021-10-08 16:00:47.000000\"\n\/\/ [\"timezone_type\"]=>\n\/\/ int(3)\n\/\/ [\"timezone\"]=>\n\/\/ string(12) \"Asia\/Jakarta\"\n\/\/}<\/code><\/pre>\n\n\n\nWhat if we want to construct a DateTime<\/code> object from a timestamp?<\/p>\n\n\n\n<?php\n\n$date = new DateTime();\n$date->setTimestamp(1634142890);\nvar_dump($date);\n\n\/\/object(DateTime)#1 (3) {\n\/\/ [\"date\"]=>\n\/\/ string(26) \"2021-10-13 23:34:50.000000\"\n\/\/ [\"timezone_type\"]=>\n\/\/ int(3)\n\/\/ [\"timezone\"]=>\n\/\/ string(12) \"Asia\/Jakarta\"\n\/\/ }<\/code><\/pre>\n\n\n\nWe don\u2019t have to create a DateTime<\/code> object if we want to convert a timestamp object to a formatted date string:<\/p>\n\n\n\n<?php\n\necho date(\"Y-m-d h:i A\", time()); \/\/ 2021-10-14 04:10 PM<\/code><\/pre>\n\n\nTimezones<\/h3>\n\n\nWe can create a DateTime<\/code> object that includes timezone information, like if we\u2019re dealing with Pacific Standard Time, Eastern Daylight Time, etc.<\/p>\n\n\n\n<?php\n\n$timezone = new DateTimeZone(\"America\/New_York\");\n$date = new DateTime(\"2021-10-13 05:00\", $timezone);\nvar_dump($date);\n\n\/\/ object(DateTime)#1 (3) {\n\/\/ [\"date\"]=>\n\/\/ string(26) \"2021-10-13 05:00:00.000000\"\n\/\/ [\"timezone_type\"]=>\n\/\/ int(3)\n\/\/ [\"timezone\"]=>\n\/\/ string(16) \"America\/New_York\"\n\/\/ }\n\n\/\/ Eastern Daylight Time, for example: New York\n$date = new DateTime(\"2021-10-13 05:00 EDT\");\nvar_dump($date);\n\n\/\/ object(DateTime)#2 (3) {\n\/\/ [\"date\"]=>\n\/\/ string(26) \"2021-10-13 05:00:00.000000\"\n\/\/ [\"timezone_type\"]=>\n\/\/ int(2)\n\/\/ [\"timezone\"]=>\n\/\/ string(3) \"EDT\"\n\/\/ }\n\n$date = new DateTime(\"2021-10-13 05:00 -04:00\");\nvar_dump($date);\n\n\/\/ object(DateTime)#1 (3) {\n\/\/ [\"date\"]=>\n\/\/ string(26) \"2021-10-13 05:00:00.000000\"\n\/\/ [\"timezone_type\"]=>\n\/\/ int(1)\n\/\/ [\"timezone\"]=>\n\/\/ string(6) \"-04:00\"\n\/\/ }<\/code><\/pre>\n\n\n\nThere are three ways to create a DateTime<\/code> object with timezone information. The timezone_type<\/code> accepts different values for each one.<\/p>\n\n\n\nBut say we want to convert a date and time that\u2019s displayed in New York\u2019s timezone to display Jakarta\u2019s timezone instead?<\/p>\n\n\n\n <?php\n\n$newYorkTimeZone = new DateTimeZone(\"America\/New_York\");\n$date = new DateTime(\"2021-11-11 05:00\", $newYorkTimeZone);\necho $date->format(\"Y-m-d h:i A\"); \/\/ 2021-11-11 05:00 AM\n$jakartaTimeZone = new DateTimeZone(\"Asia\/Jakarta\");\n$date->setTimeZone($jakartaTimeZone);\necho $date->format(\"Y-m-d h:i A\"); \/\/ 2021-11-11 05:00 PM<\/code><\/pre>\n\n\n\nWhen it\u2019s 05:00 AM in New York, it\u2019s 05:00 PM in Jakarta on the same day. Jakarta is 12 hours ahead of New York on November 11th 2021. But one month earlier, Jakarta is only 11 hours ahead of New York as shown below:<\/p>\n\n\n\n <?php\n\n$newYorkTimeZone = new DateTimeZone(\"America\/New_York\");\n$date = new DateTime(\"2021-10-11 05:00\", $newYorkTimeZone);\necho $date->format(\"Y-m-d h:i A\"); \/\/ 2021-10-11 05:00 AM\n$jakartaTimeZone = new DateTimeZone(\"Asia\/Jakarta\");\n$date->setTimeZone($jakartaTimeZone);\necho $date->format(\"Y-m-d h:i A\"); \/\/ 2021-10-11 04:00 PM<\/code><\/pre>\n\n\n\nPHP handles Daylight Saving Time for you automatically.<\/p>\n\n\n Localization<\/h3>\n\n\nThis is a common way to display date and time in the United States:<\/p>\n\n\n\n <?php\n\n$now = new DateTime();\necho $now->format(\"m\/d\/Y h:i A\"); \/\/ 10\/14\/2021 03:00 PM<\/code><\/pre>\n\n\n\nBut someone in France might prefer something more common to their locale. C\u2019est horrible<\/em>, they\u2019d complain. For one, nobody puts the month before month day, except the U.S. Second, France doesn\u2019t use AM or PM \u2014 they use the 24-hour format (e.g. 14:00 instead of 2:00 PM) like the military. This is how you make a French local happy.<\/p>\n\n\n\n<?php\n\n$now = new DateTime();\necho $now->format(\"d\/m\/Y H:i\"); \/\/ 14\/10\/2021 15:00<\/code><\/pre>\n\n\n\nBut this requires an intimate knowledge about a specific country or area. Instead, we can localize the date. To localize a date, we need to install the internationalization support for PHP. In Ubuntu, we can do this step:<\/p>\n\n\n\n $ sudo apt-get install php-intl<\/code><\/pre>\n\n\n\nTo display a date and time in French, we can use IntlDateFormatter<\/code>:<\/p>\n\n\n\n$locale = \"fr_FR.UTF-8\";\n$formatter = new IntlDateFormatter($locale, IntlDateFormatter::FULL, IntlDateFormatter::SHORT, \"Asia\/Singapore\");\n$date = new DateTime(\"2020-10-10 00:00 UTC\");\necho $formatter->format($date); \/\/ samedi 10 octobre 2020 \u00e0 08:00<\/code><\/pre>\n\n\n\nYou pass the French locale as the first parameter of IntlDateFormatter<\/code>. The second parameter is the format for the date. The third parameter is the format for the time. The timezone for displaying the date and time is in the fourth parameter.<\/p>\n\n\n\nBesides IntlDateFormatter::FULL<\/code> and IntlDateFormatter::SHORT<\/code>, other popular formats are IntlDateFormatter::NONE<\/code>, IntlDateFormatter::LONG<\/code>, and IntlDateFormatter::MEDIUM<\/code>.<\/p>\n\n\n\nIf you use IntlDateFormatter::NONE<\/code> for the time or in the third parameter, it means you don’t include the time in the format:<\/p>\n\n\n\n$locale = \"fr_FR.UTF-8\";\n$formatter = new IntlDateFormatter($locale, IntlDateFormatter::LONG, IntlDateFormatter::NONE, \"Asia\/Singapore\");\n$date = new DateTime(\"2020-10-10 00:00 UTC\");\necho $formatter->format($date); \/\/ 10 octobre 2020<\/code><\/pre>\n\n\nTime travel<\/h3>\n\n\nLet\u2019s do time travel to the past and the future. First, let\u2019s get acquainted with DateInterval<\/code>:<\/p>\n\n\n\n<?php\n\n$interval = new DateInterval(\"P4M1W2DT2H5M\");\n\n\/\/ P 4M 1W 2D T 2H 5M\n\/\/\n\/\/ P = Period interval (years, months, weeks, days)\n\/\/ 4M = 4 months\n\/\/ 1W = 1 week\n\/\/ 2D = 2 days\n\/\/\n\/\/ T = Time interval (hours, minutes, seconds)\n\/\/ 2H = 2 hours\n\/\/ 5M = 5 minutes<\/code><\/pre>\n\n\n\nThe P<\/code> and T<\/code> are to separate period interval and time interval. Here\u2019s how we can travel to the future:<\/p>\n\n\n\n<?php\n\n$date = new DateTime(\"2021-10-14\");\n$interval = new DateInterval(\"P2D\"); \/\/ 2 days\n$futureDate = $date->add($interval);\necho $futureDate->format(\"Y-m-d\"); \/\/ 2021-10-16<\/code><\/pre>\n\n\n\nAnd here\u2019s how we go back in time:<\/p>\n\n\n\n <?php\n\n$date = new DateTime(\"2021-10-14 10:00\");\n$interval = new DateInterval(\"PT6H\"); \/\/ 6 hours\n$pastDate = $date->sub($interval);\necho $pastDate->format(\"Y-m-d H:i\"); \/\/ 2021-10-14 04:00<\/code><\/pre>\n\n\n\nIf we want to time travel with the name of the weekday, we can combine the strtotime()<\/code> function and the setTimestamp()<\/code> method of a DateTime<\/code> object:<\/p>\n\n\n\n<?php\n\n$nextTuesday = strtotime(\"next tuesday\");\n$date = new DateTime(\"2021-10-14\");\n$date->setTimestamp($nextTuesday);\necho $date->format(\"Y-m-d\"); \/\/ 2021-10-19<\/code><\/pre>\n\n\n\n
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |