{"id":173,"date":"2018-07-12T11:20:15","date_gmt":"2018-07-12T11:20:15","guid":{"rendered":"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=173"},"modified":"2018-12-07T11:00:14","modified_gmt":"2018-12-07T11:00:14","slug":"numbers-and-dates-with-php","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/chapter\/numbers-and-dates-with-php\/","title":{"rendered":"Numbers and Dates with PHP"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/ctHt4Xut3Gk\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Numbers<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Numbers can be integer or double. Integer number stores whole numbers. Integer number can have positive or negative sign. By default, is it positive, so the + sign can be omitted when write positive numbers. The maximum value for integer number is \u2013 2147483648 to +2147483647. PHP_INT_MAX constant is used by php to store this value. For example,<\/p>\r\n&nbsp;\r\n\r\n$max = PHP_INT_MAX;\r\n\r\necho \"Maximum value for PHP constant is: $max\";\r\n\r\nThen the output is:\r\n\r\nMaximum value for PHP constant is: 2147483647;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Floating point numbers are also known as floats, doubles or real numbers. Floating point number stores fraction value. To store a number as a floating point number, it must be written with .(decimal point). That means, if a variable assign a value 1 then the data type of a variable is treated as integer. A variable assign a value 1. (one with decimal point) then the data type of a variable is considered as double. Exponential notation can be used in double data type. The exponential notation is consisting of two parts. Mantissa and exponent. This number is written as mantissa e exponent. Mantissa can be integer or floating point number. The exponent must be integer number. The range for exponent is -307 to +308. The exponential notation expand to m * 10e. For example, 3.5e4 expand to 3.5* 104 means 3.5 * 10000 = 35000. In the same way -3.25e-2 expands -3.25*10(-2) = -3.25 * 1\/100 = -0.0325. In exponential form the sign of mantissa determine the overall number is positive or negative and the sign of exponent specifies that the number is large or small than 1.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">If the number is outside of the range of double value, then it is converted by positive or negative infinity. Positive infinity is represented by the constant named INF. Negative infinity is represented by the constant named \u2013INF. You can directly assign infinity to a variable or the result of an operation may be infinity. Generally, most operation where one operand is infinity, the result is infinity. The exception is when a value is divide by zero, it returns zero.<\/p>\r\n&nbsp;\r\n\r\nIn case you divide a value by zero, PHP generates warning.\r\n\r\n&nbsp;\r\n\r\nTo check whether the variable or expression is infinite, there are two functions available. These functions are:\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" style=\"height: 133px\" border=\"1\" width=\"545\">\r\n<tbody>\r\n<tr style=\"height: 14px\">\r\n<td style=\"height: 14px;width: 149.063px\"><strong>Function<\/strong><\/td>\r\n<td style=\"height: 14px;width: 367.063px\"><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr style=\"height: 14px\">\r\n<td style=\"height: 14px;width: 149.063px\">is_infinite($value)<\/td>\r\n<td style=\"height: 14px;width: 367.063px\">Reruns true if the $value is infinite else false.<\/td>\r\n<\/tr>\r\n<tr style=\"height: 14px\">\r\n<td style=\"height: 14px;width: 149.063px\">is_finite($value)<\/td>\r\n<td style=\"height: 14px;width: 367.063px\">Reruns false if the $value is infinite else true.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nFor example:\r\n\r\n&nbsp;\r\n\r\n$testinf = is_infinite(10*INF);\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\">\u00a0 \u00a0echo \"&lt;br&gt; the value of '10 * INF' is : $testinf\";<\/p>\r\n<p style=\"text-align: center\">will display 1 on browser.<\/p>\r\n<p style=\"text-align: center\"><strong>In-Built useful Number Functions<\/strong><\/p>\r\n<img class=\"alignnone size-full wp-image-176 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-93.png\" alt=\"\" width=\"660\" height=\"428\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>Type Casting:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Type casting is used to convert one data type to another type. To type cast a value, write the data type in parentheses before the value. Below are few examples of type casting.<\/p>\r\n&nbsp;\r\n\r\n\/\/\u00a0\u00a0 Type casting\r\n\r\n$value = 31.8;\r\n\r\n$intcast = (int)$value;\r\n\r\necho \"&lt;br&gt; The original $value after casting to integer becomes: $intcast \";\r\n\r\n\/\/with string\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$value = \"31.8 Kilometers\"; \/\/ strings after the number is ignored $strint = (int)$value;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; The original '$value' after casting to integer becomes: $strint \"; \/\/,<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$value = \"31,000 Kilometers\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$strint = (int)$value;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; The original '$value' after casting to integer becomes: $strint \"; \/\/ string and number_format<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$value = \"Kilometres 31,000\"; \/\/ strings which not start with number is converted to 0 $strint = (int)$value;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; The original '$value' after casting to integer becomes: $strint \"; \/\/only string<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$value = \"Kilometres\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$strint = (int)$value;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; The original '$value' after casting to integer becomes: $strint \"; \/\/out of range value<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$value = '10000000000';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$strint = (int)$value;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; The original '$value' after casting to integer becomes: $strint \";<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">The output is:<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThe original 31.8 after casting to integer becomes: 31\r\n\r\nThe original '31.8 Kilometers' after casting to integer becomes: 31\r\n\r\nThe original '31,000 Kilometers' after casting to integer becomes: 31\r\n\r\nThe original 'Kilometres 31,000' after casting to integer becomes: 0\r\n\r\nThe original 'Kilometres' after casting to integer becomes: 0\r\n\r\nThe original '10000000000' after casting to integer becomes: 2147483647\r\n\r\n&nbsp;\r\n\r\n<strong>Sprintf<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The sprint function is used to format strings and numbers. The general syntax of sprintf function is:<\/p>\r\n&nbsp;\r\n\r\nsprintf(format, $val1 [,$val2 \u2026.])\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The format parameter can take any one value of for formatting $val. The format parameter can take any one of the following data type<\/p>\r\n&nbsp;\r\n\r\ns : string\r\n\r\nd : integer\r\n\r\nf:\u00a0 double\/decimal value\r\n\r\ne:\u00a0\u00a0 exponential notation\r\n<p style=\"text-align: justify\">c:\u00a0\u00a0 convert integer value corresponding to equivalent ASCII character. The format-code must start with % symbol end ends with data type. Few examples for sprintf function is shown below.<\/p>\r\n\/\/sprintf examples\r\n\r\n$msg = sprintf(\"The book on %s has %d pages\",\"PHP\", 543);\r\n\r\necho \"&lt;br&gt; $msg\";\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$s1 = sprintf(\"%c\",65);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; The ASCII character for value 65 is : $s1\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$s2 = sprintf(\"%06d\",42);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; The value 42 padded with 0 for length 0f 6 is: $s2\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$date = sprintf(\"%02d-%02d-%04d\", 3,5,2015);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Print the date in DD-MM-YYYY format : $date\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$s4 = sprintf(\"%09.2f\", 123.4567890);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; The value is: $s4\";<\/span>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">The output of the above code is:<\/span>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThe book on PHP has 543 pages\r\n\r\nThe ASCII character for value 65 is : A\r\n\r\nThe value 42 paddedwith 0 for length 0f 6 is: 000042\r\n\r\nPrint the date in DD-MM-YYYY format : 03-05-2015\r\n\r\nThe value is: 000123.46\r\n\r\n&nbsp;\r\n\r\n<strong>Date:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">PHP is used timestamp to display date and time. Timestamp stores an integer value, which stores the numbers of second since midnight on January 1, 1970 GMT (Greenwich Mean Time). This point of time is known as the Unix Epoch. Timestamp stored as a 32-bit signed integer number by most systems in the world. Hence these system timestamp can range from December 13, 1901 to January 19, 2038. When the system uses timestamps, reaches its upper limit January 19, 2038 its rolls over to the lower limit in 1901. When this may happens, it may create many problems. This problem is known as the year 2038 problem. This problem is similar to Y2K problem, so year 2038 problem is also known as Y2K38 problem. One way to solve the Y2K38 problem is to use DateTime objects. The DateTime object is explain later on.<\/p>\r\n&nbsp;\r\n\r\nTo create and format timestamp date( ) is used. The general syntax for date( ) function is:\r\n\r\n&nbsp;\r\n\r\nDate($format [, $timestamp])\r\n\r\n&nbsp;\r\n\r\nWhere $format represents various formatting option for date and $ts is a timestamp.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The date function returned the formatted timestamp. The format is specified in $format. By default the time stamp is current date and time. The $timestamp can be specified for any date or time.<\/p>\r\n&nbsp;\r\n\r\nFor example:\r\n\r\n&nbsp;\r\n\r\n$d = date('j-n-Y');\r\n\r\necho \"&lt;br&gt;Today is $d\";\r\n\r\ndisplay\r\n\r\nToday is 31-5-2015.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">There are many formatting options available. Following table shows few formatting options.<\/p>\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n<table class=\"aligncenter\" style=\"height: 725px\" border=\"1\" width=\"568\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 79.0625px\"><strong>Format<\/strong><\/td>\r\n<td style=\"width: 460.063px\"><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">j<\/td>\r\n<td style=\"width: 460.063px\">Day of month without leading zero.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">d<\/td>\r\n<td style=\"width: 460.063px\">Day of month with leading zero.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">D<\/td>\r\n<td style=\"width: 460.063px\">Day of week in three letters (i.e.: Mon).<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">l<\/td>\r\n<td style=\"width: 460.063px\">Day of week full-name (i.e.: Monday)<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">n<\/td>\r\n<td style=\"width: 460.063px\">Month number without leading zero<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">m<\/td>\r\n<td style=\"width: 460.063px\">Month number with leading zero<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">M<\/td>\r\n<td style=\"width: 460.063px\">Month name in three letters.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">S<\/td>\r\n<td style=\"width: 460.063px\">Ordinal Suffix of a day<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">F<\/td>\r\n<td style=\"width: 460.063px\">Full Month Name.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">y<\/td>\r\n<td style=\"width: 460.063px\">Year in two digits.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">Y<\/td>\r\n<td style=\"width: 460.063px\">Year in four digits.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">L<\/td>\r\n<td style=\"width: 460.063px\">Leap year or not. For leap year it is return 1<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\"><\/td>\r\n<td style=\"width: 460.063px\">otherwise 0.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">h<\/td>\r\n<td style=\"width: 460.063px\">Hours in 24 hour format without leading zero.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">g<\/td>\r\n<td style=\"width: 460.063px\">Hours in 12 hour format without leading zero.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">H<\/td>\r\n<td style=\"width: 460.063px\">Hours in 24 hour format with leading zero.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">G<\/td>\r\n<td style=\"width: 460.063px\">Hours in 12 hour format with leading zero<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">i<\/td>\r\n<td style=\"width: 460.063px\">Minutes with leading zero<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">s<\/td>\r\n<td style=\"width: 460.063px\">Seconds with leading zero<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">A<\/td>\r\n<td style=\"width: 460.063px\">AM\/PM in uppercase<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">a<\/td>\r\n<td style=\"width: 460.063px\">am\/pm in lowercase<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 79.0625px\">U<\/td>\r\n<td style=\"width: 460.063px\">Seconds since unix epoch<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nFor example,\r\n\r\n$d = date('j-n-Y \\a\\t H:i A ');\r\n\r\necho \"&lt;br&gt;Today is $d\";\r\n\r\nwill display\r\n\r\nToday is 31-5-2015 at 08:57 AM\r\n\r\nToday is 31-5-2015 at 08:57 AM\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>Useful functions for working with Timestamps<\/strong><\/p>\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 267.063px\"><strong>Function<\/strong><\/td>\r\n<td style=\"width: 303.063px\"><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 267.063px\">time()<\/td>\r\n<td style=\"width: 303.063px\">Returns current date time as timestamp<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 267.063px\" rowspan=\"4\">mktime([$h [,$m [,$s [,$M [,$D [,$Y]]]]]])\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;<\/td>\r\n<td style=\"width: 303.063px\" rowspan=\"4\">Returns\u00a0\u00a0 timestamp\u00a0\u00a0 based\u00a0\u00a0 on\u00a0\u00a0 the\r\n\r\nparameters\u00a0 passed.\u00a0 If\u00a0 any\u00a0 parameter\u00a0 is\r\n\r\nskipped, then the value of current date and\r\n\r\ntime is set.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 267.063px\" rowspan=\"2\">checkdate($M,$D,$Y)\r\n\r\n&nbsp;<\/td>\r\n<td style=\"width: 303.063px\" rowspan=\"2\">Returns true if the $M (month), $D (date)\r\n\r\nand $Y (year) provide valid date.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 267.063px\" rowspan=\"2\">getdate([$ts])\r\n\r\n&nbsp;<\/td>\r\n<td style=\"width: 303.063px\" rowspan=\"2\">Returns\u00a0 an\u00a0 array\u00a0 having\u00a0 parts\u00a0 of\u00a0 time\r\n\r\nstamp. If $ts is omitted, it uses current\u00a0date and time<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Following code shows use of timestamp functions:<\/strong>\r\n\r\n&nbsp;\r\n\r\n$ts = time();\r\n\r\n&nbsp;\r\n\r\necho \"&lt;br&gt;The value of current time stamp is : $ts\"; $mts = mktime(10, 10, 00,13, 4, 2012 );\r\n\r\n&nbsp;\r\n\r\necho \"&lt;br&gt;The new time stamp has value: $mts\"; $chkdt = checkdate(11,30,2015);\r\n\r\n&nbsp;\r\n\r\necho \"&lt;br&gt; 30 Novemebr 2015 by checkdate returns: $chkdt &lt;br&gt;\";\r\n\r\n&nbsp;\r\n\r\nThen the output will be:\r\n\r\n&nbsp;\r\n\r\nThe value of current time stamp is : 1433058159\r\n\r\n&nbsp;\r\n\r\nThe new time stamp has value: 1357290600\r\n\r\n&nbsp;\r\n\r\n30 November 2015 by checkdate returns: 1\r\n\r\n&nbsp;\r\n\r\nYou can find many timestamp functions at PHP manual on the web.\r\n\r\n&nbsp;\r\n\r\n<strong>Strtotime function in PHP<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe strtotime function returns timestamp for a string. The general syntax is\r\n\r\n&nbsp;\r\n\r\nStrtotime($str [,$ts])\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">If timestamp $ts is omitted, then this functions consider current date and time as timestamp. Whenever timestamp $ts is specified, then it works relative to specified timestamp $ts. The strtotime functions supports most of the date and time formats and most relative time specifications.<\/p>\r\n&nbsp;\r\n\r\nBelow shows few examples of absolute templates used with strtotime function.\r\n\r\n&nbsp;\r\n\r\n$date1 = strtotime('2015-06-01'); \/\/Mon June 1 2015 00:00\r\n\r\n$date2 = strtotime(\u2018Jun 1\u2019); \/\/Mon June 1 2015 00:00\r\n\r\n$date3 = strtotime(\u20186:45\u2019); \/\/ Mon June 1 2015 6:45\r\n\r\n&nbsp;\r\n\r\nThe following code snippets represent relative templates with strtotime function.\r\n\r\n&nbsp;\r\n\r\n\/\/Assume that the current date time is 06\/01\/2015 10:44:00 PM (June 1, 2015)\r\n\r\n&nbsp;\r\n\r\n$date4 = strtotime('+1 hour');\u00a0\u00a0\u00a0 \/\/Mon 06\/01\/2015 11:44:00 PM\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 $date5 = strtotime(\u2018-2 days\u2019);\u00a0\u00a0\u00a0 \/\/Sat 05\/30\/2015 11:44:00 PM\r\n\r\n&nbsp;\r\n\r\n$date6 = strtotime(\u2018next Sunday\u2019)\u00a0 \/\/Sun 06\/07\/2015 00:00\r\n\r\n&nbsp;\r\n\r\n<strong>Datetime<\/strong>\r\n<ul>\r\n \t<li style=\"text-align: justify\">The datetime class facilitate to work with date and time using an object-oriented way.<\/li>\r\n \t<li style=\"text-align: justify\">\u00a0To create datetime object pass any string to the DateTime class constructor. The string passed in the constructor is string which is any string which accepted by the strtotime function.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nThe general syntax of creating datetime object is:\r\n\r\nnew DateTime([$param])\r\n\r\nFollowing table shows the generally used methods of DateTime object.\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 129.063px\"><strong>Method<\/strong><\/td>\r\n<td style=\"width: 475.063px\"><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 129.063px\" rowspan=\"2\">format($format)\r\n\r\n&nbsp;<\/td>\r\n<td style=\"width: 475.063px\" rowspan=\"2\">Returns a formatting string with $format. The formatting options are\r\n\r\nthe same as used with the date function.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 129.063px\">setTime($h, $m, $s)<\/td>\r\n<td style=\"width: 475.063px\">Set time using hour $h, minute $m and second $s<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 129.063px\">setDate($y, $m, $d)<\/td>\r\n<td style=\"width: 475.063px\">Set date using year $y, month $m and date $d<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 129.063px\" rowspan=\"3\">modify($str)\r\n\r\n&nbsp;\r\n\r\n&nbsp;<\/td>\r\n<td style=\"width: 475.063px\" rowspan=\"3\">Modify the date and time based on the formatting string $str. This\r\n\r\nfunction works similar to the strtotime function for a relative date or\r\n\r\ntime.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 129.063px\">getTimestamp()<\/td>\r\n<td style=\"width: 475.063px\">Gets the date and time as timestamp.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 129.063px\">setTimestamp($ts)<\/td>\r\n<td style=\"width: 475.063px\">Sets the date and time using timestamp $ts.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To get the current date and time, do not required passing any parameter. Following code snippet displays current date and time.<\/p>\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$dt = new DateTime();\r\n\r\n$disp = $dt-&gt;format('d-m-Y h:m:s ');\r\n\r\necho $disp;\r\n\r\n?&gt;\r\n\r\n22-11-2015 01:11:47 [Considering today is November 22,2015]\r\n\r\nThe following code shows the use of setTime function.\r\n\r\n&lt;?php\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0$t = new DateTime();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo $t-&gt;format('d-m-Y h:m:s');<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$t-&gt;setTime(4,07,15);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt;\".$t-&gt;format('d-m-Y g:i:s a');<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n22-11-2015 03:11:14\r\n\r\n22-11-2015 4:07:15 am\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To modify the date and time modify function is used. Before using the modify function, clone the object and apply the modify function.<\/p>\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$invoicedate = new DateTime('2014-03-23 14:30:18'); $due_date = clone $invoicedate;\r\n\r\necho \"&lt;dr&gt; Invoice date is:\".$invoicedate-&gt;format('M j Y g:i:s a'); $due_date-&gt;modify('+4 weeks');\r\n\r\necho \"&lt;br&gt; Due Date (After 4 weeks of invoice date) is: \".$due_date-&gt;format('M j Y \\a\\t g:i:s a'); ?&gt;\r\n\r\nInvoice date is:Mar 23 2014 2:30:18 pm\r\n\r\nDue Date (After 4 weeks of invoice date) is: Apr 20 2014 at 2:30:18 pm\r\n\r\nA timestamp can be converted to datetime object.\r\n\r\n&lt;?php\r\n\r\n$tomorrow = strtotime('tomorrow 8am');\r\n\r\n$nextday = new DateTime();\r\n\r\n$nextday-&gt;setTimestamp($tomorrow);\r\n\r\necho $nextday-&gt;getTimestamp();\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\nThe output of the above code is:\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0 1448262000<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>DateInterval Class<\/strong>\r\n<ul>\r\n \t<li>The date interval class is used to represent a span of time.<\/li>\r\n \t<li>The syntax to create dateinterval class object<\/li>\r\n \t<li>New DateInterval($str)<\/li>\r\n \t<li>The string $str must be start with P (period of time for Date)<\/li>\r\n \t<li>The string $str can take T to represent period of time.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nFollowing table represents the parts of the interval string.\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-177 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-94.png\" alt=\"\" width=\"649\" height=\"336\" \/>\u00a0 \u00a0 Following code snippet using DateInterval object\r\n$i1 = new DateInterval('P30D'); \/\/ 30 Days Interval\r\nprint_r($i1);\r\n<p style=\"text-align: justify\">DateInterval Object ( [y] =&gt; 0 [m] =&gt; 0 [d] =&gt; 30 [h] =&gt; 0 [i] =&gt; 0 [s] =&gt; 0 [weekday] =&gt; 0 [weekday_behavior] =&gt; 0 [first_last_day_of] =&gt; 0 [invert] =&gt; 0 [days] =&gt; [special_type] =&gt; 0 [special_amount] =&gt; 0 [have_weekday_relative] =&gt; 0 [have_special_relative] =&gt; 0 )<\/p>\r\n&nbsp;\r\n\r\n<strong>Below are few code which demonstrate the use of DateInterval<\/strong>\r\n\r\n&nbsp;\r\n\r\n$i2 = new DateInterval('P1Y23D'); \/\/ 1 Year 23 Days\r\n\r\n$i3 = new DateInterval('PT1H2M12S'); \/\/ 1 Hour 2 Minutes 12 Second\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$i4 = new DateInterval('P1Y23DT2H5M12S'); \/\/ 1 Year 23 Days 2 Hours 5 Minutes 12 Seconds<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To display the interval format function of the DateInterval object is used. The format method takes format parameter as string. It allows following format code.<\/p>\r\n&nbsp;\r\n\r\nSyntax:\r\n\r\n&nbsp;\r\n\r\nFormat($format)\r\n\r\n&nbsp;\r\n\r\nThe codes for formatting are described below.\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>Code<\/strong><\/td>\r\n<td><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>%R<\/td>\r\n<td>Sign of the interval + for positive interval value and \u2013 for negative interval value.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>%y<\/td>\r\n<td>Years<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>%m<\/td>\r\n<td>Months<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>%d<\/td>\r\n<td>Days<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>%h<\/td>\r\n<td>Hours<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>%i<\/td>\r\n<td>Minutes<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>%s<\/td>\r\n<td>Seconds<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nFor example,\r\n\r\n&nbsp;\r\n\r\n$i5 = new DateInterval('P2M10D');\r\n\r\necho \"&lt;br&gt;\".$i5-&gt;format('%m months, %d days');\r\n\r\nwill print\r\n\r\n2 months, 10 days\r\n\r\n&nbsp;\r\n\r\n<strong>Use the DateInterval and DateTime classes together<\/strong>\r\n\r\n&nbsp;\r\n\r\nFollowing table shows 3 more methods of DateTime object which uses DateInterval objects.\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><strong>Name<\/strong><\/td>\r\n<td><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>add($interval)<\/td>\r\n<td>Add the $interval time<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>sub($interval)<\/td>\r\n<td>Subtract the $interval time<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"2\">diff($date)\r\n\r\n&nbsp;<\/td>\r\n<td rowspan=\"2\">Returns a DateInterval object which represents the amount of time between the current\r\n\r\nDatetime object and $date.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nThe following code shows example for sub method.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$votingage = new DateInterval('P18Y');\r\n\r\n$dob = new DateTime();\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$dob-&gt;sub($votingage);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo '&lt;br&gt; You can vote only if you were born on or before: '. $dob-&gt;format('j\/n\/Y');<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\nThe output will be\r\n\r\n&nbsp;\r\n\r\nYou can vote only if you were born on or before: 23\/11\/1997\r\n\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Numbers and Dates with PHP<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/ctHt4Xut3Gk\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<strong>References:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\r\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional, Apress<\/p>\r\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass:Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\r\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O'Reilly Media<\/p>\r\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove: Professional PHP,Wrox<\/p>\r\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\r\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\r\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\r\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\r\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide, Pearson Education<\/p>\r\n<p style=\"text-align: justify\">11. http:\/\/www.php.net\/<\/p>\r\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\r\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/ctHt4Xut3Gk\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Numbers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Numbers can be integer or double. Integer number stores whole numbers. Integer number can have positive or negative sign. By default, is it positive, so the + sign can be omitted when write positive numbers. The maximum value for integer number is \u2013 2147483648 to +2147483647. PHP_INT_MAX constant is used by php to store this value. For example,<\/p>\n<p>&nbsp;<\/p>\n<p>$max = PHP_INT_MAX;<\/p>\n<p>echo &#8220;Maximum value for PHP constant is: $max&#8221;;<\/p>\n<p>Then the output is:<\/p>\n<p>Maximum value for PHP constant is: 2147483647;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Floating point numbers are also known as floats, doubles or real numbers. Floating point number stores fraction value. To store a number as a floating point number, it must be written with .(decimal point). That means, if a variable assign a value 1 then the data type of a variable is treated as integer. A variable assign a value 1. (one with decimal point) then the data type of a variable is considered as double. Exponential notation can be used in double data type. The exponential notation is consisting of two parts. Mantissa and exponent. This number is written as mantissa e exponent. Mantissa can be integer or floating point number. The exponent must be integer number. The range for exponent is -307 to +308. The exponential notation expand to m * 10e. For example, 3.5e4 expand to 3.5* 104 means 3.5 * 10000 = 35000. In the same way -3.25e-2 expands -3.25*10(-2) = -3.25 * 1\/100 = -0.0325. In exponential form the sign of mantissa determine the overall number is positive or negative and the sign of exponent specifies that the number is large or small than 1.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">If the number is outside of the range of double value, then it is converted by positive or negative infinity. Positive infinity is represented by the constant named INF. Negative infinity is represented by the constant named \u2013INF. You can directly assign infinity to a variable or the result of an operation may be infinity. Generally, most operation where one operand is infinity, the result is infinity. The exception is when a value is divide by zero, it returns zero.<\/p>\n<p>&nbsp;<\/p>\n<p>In case you divide a value by zero, PHP generates warning.<\/p>\n<p>&nbsp;<\/p>\n<p>To check whether the variable or expression is infinite, there are two functions available. These functions are:<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\" style=\"height: 133px; width: 545px;\">\n<tbody>\n<tr style=\"height: 14px\">\n<td style=\"height: 14px;width: 149.063px\"><strong>Function<\/strong><\/td>\n<td style=\"height: 14px;width: 367.063px\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 14px\">\n<td style=\"height: 14px;width: 149.063px\">is_infinite($value)<\/td>\n<td style=\"height: 14px;width: 367.063px\">Reruns true if the $value is infinite else false.<\/td>\n<\/tr>\n<tr style=\"height: 14px\">\n<td style=\"height: 14px;width: 149.063px\">is_finite($value)<\/td>\n<td style=\"height: 14px;width: 367.063px\">Reruns false if the $value is infinite else true.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>For example:<\/p>\n<p>&nbsp;<\/p>\n<p>$testinf = is_infinite(10*INF);<\/p>\n<\/div>\n<div>\n<p style=\"text-align: center\">\u00a0 \u00a0echo &#8220;&lt;br&gt; the value of &#8217;10 * INF&#8217; is : $testinf&#8221;;<\/p>\n<p style=\"text-align: center\">will display 1 on browser.<\/p>\n<p style=\"text-align: center\"><strong>In-Built useful Number Functions<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-176 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-93.png\" alt=\"\" width=\"660\" height=\"428\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-93.png 660w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-93-300x195.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-93-65x42.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-93-225x146.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-93-350x227.png 350w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Type Casting:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Type casting is used to convert one data type to another type. To type cast a value, write the data type in parentheses before the value. Below are few examples of type casting.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u00a0\u00a0 Type casting<\/p>\n<p>$value = 31.8;<\/p>\n<p>$intcast = (int)$value;<\/p>\n<p>echo &#8220;&lt;br&gt; The original $value after casting to integer becomes: $intcast &#8220;;<\/p>\n<p>\/\/with string<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$value = &#8220;31.8 Kilometers&#8221;; \/\/ strings after the number is ignored $strint = (int)$value;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; The original &#8216;$value&#8217; after casting to integer becomes: $strint &#8220;; \/\/,<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$value = &#8220;31,000 Kilometers&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$strint = (int)$value;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; The original &#8216;$value&#8217; after casting to integer becomes: $strint &#8220;; \/\/ string and number_format<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$value = &#8220;Kilometres 31,000&#8221;; \/\/ strings which not start with number is converted to 0 $strint = (int)$value;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; The original &#8216;$value&#8217; after casting to integer becomes: $strint &#8220;; \/\/only string<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$value = &#8220;Kilometres&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$strint = (int)$value;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; The original &#8216;$value&#8217; after casting to integer becomes: $strint &#8220;; \/\/out of range value<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$value = &#8216;10000000000&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$strint = (int)$value;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; The original &#8216;$value&#8217; after casting to integer becomes: $strint &#8220;;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">The output is:<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>The original 31.8 after casting to integer becomes: 31<\/p>\n<p>The original &#8216;31.8 Kilometers&#8217; after casting to integer becomes: 31<\/p>\n<p>The original &#8216;31,000 Kilometers&#8217; after casting to integer becomes: 31<\/p>\n<p>The original &#8216;Kilometres 31,000&#8217; after casting to integer becomes: 0<\/p>\n<p>The original &#8216;Kilometres&#8217; after casting to integer becomes: 0<\/p>\n<p>The original &#8216;10000000000&#8217; after casting to integer becomes: 2147483647<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Sprintf<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The sprint function is used to format strings and numbers. The general syntax of sprintf function is:<\/p>\n<p>&nbsp;<\/p>\n<p>sprintf(format, $val1 [,$val2 \u2026.])<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The format parameter can take any one value of for formatting $val. The format parameter can take any one of the following data type<\/p>\n<p>&nbsp;<\/p>\n<p>s : string<\/p>\n<p>d : integer<\/p>\n<p>f:\u00a0 double\/decimal value<\/p>\n<p>e:\u00a0\u00a0 exponential notation<\/p>\n<p style=\"text-align: justify\">c:\u00a0\u00a0 convert integer value corresponding to equivalent ASCII character. The format-code must start with % symbol end ends with data type. Few examples for sprintf function is shown below.<\/p>\n<p>\/\/sprintf examples<\/p>\n<p>$msg = sprintf(&#8220;The book on %s has %d pages&#8221;,&#8221;PHP&#8221;, 543);<\/p>\n<p>echo &#8220;&lt;br&gt; $msg&#8221;;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$s1 = sprintf(&#8220;%c&#8221;,65);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; The ASCII character for value 65 is : $s1&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$s2 = sprintf(&#8220;%06d&#8221;,42);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; The value 42 padded with 0 for length 0f 6 is: $s2&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$date = sprintf(&#8220;%02d-%02d-%04d&#8221;, 3,5,2015);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Print the date in DD-MM-YYYY format : $date&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$s4 = sprintf(&#8220;%09.2f&#8221;, 123.4567890);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; The value is: $s4&#8221;;<\/span><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">The output of the above code is:<\/span><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>The book on PHP has 543 pages<\/p>\n<p>The ASCII character for value 65 is : A<\/p>\n<p>The value 42 paddedwith 0 for length 0f 6 is: 000042<\/p>\n<p>Print the date in DD-MM-YYYY format : 03-05-2015<\/p>\n<p>The value is: 000123.46<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Date:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">PHP is used timestamp to display date and time. Timestamp stores an integer value, which stores the numbers of second since midnight on January 1, 1970 GMT (Greenwich Mean Time). This point of time is known as the Unix Epoch. Timestamp stored as a 32-bit signed integer number by most systems in the world. Hence these system timestamp can range from December 13, 1901 to January 19, 2038. When the system uses timestamps, reaches its upper limit January 19, 2038 its rolls over to the lower limit in 1901. When this may happens, it may create many problems. This problem is known as the year 2038 problem. This problem is similar to Y2K problem, so year 2038 problem is also known as Y2K38 problem. One way to solve the Y2K38 problem is to use DateTime objects. The DateTime object is explain later on.<\/p>\n<p>&nbsp;<\/p>\n<p>To create and format timestamp date( ) is used. The general syntax for date( ) function is:<\/p>\n<p>&nbsp;<\/p>\n<p>Date($format [, $timestamp])<\/p>\n<p>&nbsp;<\/p>\n<p>Where $format represents various formatting option for date and $ts is a timestamp.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The date function returned the formatted timestamp. The format is specified in $format. By default the time stamp is current date and time. The $timestamp can be specified for any date or time.<\/p>\n<p>&nbsp;<\/p>\n<p>For example:<\/p>\n<p>&nbsp;<\/p>\n<p>$d = date(&#8216;j-n-Y&#8217;);<\/p>\n<p>echo &#8220;&lt;br&gt;Today is $d&#8221;;<\/p>\n<p>display<\/p>\n<p>Today is 31-5-2015.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">There are many formatting options available. Following table shows few formatting options.<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<table class=\"aligncenter\" style=\"height: 725px; width: 568px;\">\n<tbody>\n<tr>\n<td style=\"width: 79.0625px\"><strong>Format<\/strong><\/td>\n<td style=\"width: 460.063px\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">j<\/td>\n<td style=\"width: 460.063px\">Day of month without leading zero.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">d<\/td>\n<td style=\"width: 460.063px\">Day of month with leading zero.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">D<\/td>\n<td style=\"width: 460.063px\">Day of week in three letters (i.e.: Mon).<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">l<\/td>\n<td style=\"width: 460.063px\">Day of week full-name (i.e.: Monday)<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">n<\/td>\n<td style=\"width: 460.063px\">Month number without leading zero<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">m<\/td>\n<td style=\"width: 460.063px\">Month number with leading zero<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">M<\/td>\n<td style=\"width: 460.063px\">Month name in three letters.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">S<\/td>\n<td style=\"width: 460.063px\">Ordinal Suffix of a day<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">F<\/td>\n<td style=\"width: 460.063px\">Full Month Name.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">y<\/td>\n<td style=\"width: 460.063px\">Year in two digits.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">Y<\/td>\n<td style=\"width: 460.063px\">Year in four digits.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">L<\/td>\n<td style=\"width: 460.063px\">Leap year or not. For leap year it is return 1<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\"><\/td>\n<td style=\"width: 460.063px\">otherwise 0.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">h<\/td>\n<td style=\"width: 460.063px\">Hours in 24 hour format without leading zero.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">g<\/td>\n<td style=\"width: 460.063px\">Hours in 12 hour format without leading zero.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">H<\/td>\n<td style=\"width: 460.063px\">Hours in 24 hour format with leading zero.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">G<\/td>\n<td style=\"width: 460.063px\">Hours in 12 hour format with leading zero<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">i<\/td>\n<td style=\"width: 460.063px\">Minutes with leading zero<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">s<\/td>\n<td style=\"width: 460.063px\">Seconds with leading zero<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">A<\/td>\n<td style=\"width: 460.063px\">AM\/PM in uppercase<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">a<\/td>\n<td style=\"width: 460.063px\">am\/pm in lowercase<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 79.0625px\">U<\/td>\n<td style=\"width: 460.063px\">Seconds since unix epoch<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>For example,<\/p>\n<p>$d = date(&#8216;j-n-Y \\a\\t H:i A &#8216;);<\/p>\n<p>echo &#8220;&lt;br&gt;Today is $d&#8221;;<\/p>\n<p>will display<\/p>\n<p>Today is 31-5-2015 at 08:57 AM<\/p>\n<p>Today is 31-5-2015 at 08:57 AM<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>Useful functions for working with Timestamps<\/strong><\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td style=\"width: 267.063px\"><strong>Function<\/strong><\/td>\n<td style=\"width: 303.063px\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 267.063px\">time()<\/td>\n<td style=\"width: 303.063px\">Returns current date time as timestamp<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 267.063px\" rowspan=\"4\">mktime([$h [,$m [,$s [,$M [,$D [,$Y]]]]]])<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/td>\n<td style=\"width: 303.063px\" rowspan=\"4\">Returns\u00a0\u00a0 timestamp\u00a0\u00a0 based\u00a0\u00a0 on\u00a0\u00a0 the<\/p>\n<p>parameters\u00a0 passed.\u00a0 If\u00a0 any\u00a0 parameter\u00a0 is<\/p>\n<p>skipped, then the value of current date and<\/p>\n<p>time is set.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 267.063px\" rowspan=\"2\">checkdate($M,$D,$Y)<\/p>\n<p>&nbsp;<\/td>\n<td style=\"width: 303.063px\" rowspan=\"2\">Returns true if the $M (month), $D (date)<\/p>\n<p>and $Y (year) provide valid date.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 267.063px\" rowspan=\"2\">getdate([$ts])<\/p>\n<p>&nbsp;<\/td>\n<td style=\"width: 303.063px\" rowspan=\"2\">Returns\u00a0 an\u00a0 array\u00a0 having\u00a0 parts\u00a0 of\u00a0 time<\/p>\n<p>stamp. If $ts is omitted, it uses current\u00a0date and time<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Following code shows use of timestamp functions:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>$ts = time();<\/p>\n<p>&nbsp;<\/p>\n<p>echo &#8220;&lt;br&gt;The value of current time stamp is : $ts&#8221;; $mts = mktime(10, 10, 00,13, 4, 2012 );<\/p>\n<p>&nbsp;<\/p>\n<p>echo &#8220;&lt;br&gt;The new time stamp has value: $mts&#8221;; $chkdt = checkdate(11,30,2015);<\/p>\n<p>&nbsp;<\/p>\n<p>echo &#8220;&lt;br&gt; 30 Novemebr 2015 by checkdate returns: $chkdt &lt;br&gt;&#8221;;<\/p>\n<p>&nbsp;<\/p>\n<p>Then the output will be:<\/p>\n<p>&nbsp;<\/p>\n<p>The value of current time stamp is : 1433058159<\/p>\n<p>&nbsp;<\/p>\n<p>The new time stamp has value: 1357290600<\/p>\n<p>&nbsp;<\/p>\n<p>30 November 2015 by checkdate returns: 1<\/p>\n<p>&nbsp;<\/p>\n<p>You can find many timestamp functions at PHP manual on the web.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Strtotime function in PHP<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The strtotime function returns timestamp for a string. The general syntax is<\/p>\n<p>&nbsp;<\/p>\n<p>Strtotime($str [,$ts])<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">If timestamp $ts is omitted, then this functions consider current date and time as timestamp. Whenever timestamp $ts is specified, then it works relative to specified timestamp $ts. The strtotime functions supports most of the date and time formats and most relative time specifications.<\/p>\n<p>&nbsp;<\/p>\n<p>Below shows few examples of absolute templates used with strtotime function.<\/p>\n<p>&nbsp;<\/p>\n<p>$date1 = strtotime(&#8216;2015-06-01&#8217;); \/\/Mon June 1 2015 00:00<\/p>\n<p>$date2 = strtotime(\u2018Jun 1\u2019); \/\/Mon June 1 2015 00:00<\/p>\n<p>$date3 = strtotime(\u20186:45\u2019); \/\/ Mon June 1 2015 6:45<\/p>\n<p>&nbsp;<\/p>\n<p>The following code snippets represent relative templates with strtotime function.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Assume that the current date time is 06\/01\/2015 10:44:00 PM (June 1, 2015)<\/p>\n<p>&nbsp;<\/p>\n<p>$date4 = strtotime(&#8216;+1 hour&#8217;);\u00a0\u00a0\u00a0 \/\/Mon 06\/01\/2015 11:44:00 PM<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 $date5 = strtotime(\u2018-2 days\u2019);\u00a0\u00a0\u00a0 \/\/Sat 05\/30\/2015 11:44:00 PM<\/p>\n<p>&nbsp;<\/p>\n<p>$date6 = strtotime(\u2018next Sunday\u2019)\u00a0 \/\/Sun 06\/07\/2015 00:00<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Datetime<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify\">The datetime class facilitate to work with date and time using an object-oriented way.<\/li>\n<li style=\"text-align: justify\">\u00a0To create datetime object pass any string to the DateTime class constructor. The string passed in the constructor is string which is any string which accepted by the strtotime function.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>The general syntax of creating datetime object is:<\/p>\n<p>new DateTime([$param])<\/p>\n<p>Following table shows the generally used methods of DateTime object.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td style=\"width: 129.063px\"><strong>Method<\/strong><\/td>\n<td style=\"width: 475.063px\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 129.063px\" rowspan=\"2\">format($format)<\/p>\n<p>&nbsp;<\/td>\n<td style=\"width: 475.063px\" rowspan=\"2\">Returns a formatting string with $format. The formatting options are<\/p>\n<p>the same as used with the date function.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 129.063px\">setTime($h, $m, $s)<\/td>\n<td style=\"width: 475.063px\">Set time using hour $h, minute $m and second $s<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 129.063px\">setDate($y, $m, $d)<\/td>\n<td style=\"width: 475.063px\">Set date using year $y, month $m and date $d<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 129.063px\" rowspan=\"3\">modify($str)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/td>\n<td style=\"width: 475.063px\" rowspan=\"3\">Modify the date and time based on the formatting string $str. This<\/p>\n<p>function works similar to the strtotime function for a relative date or<\/p>\n<p>time.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 129.063px\">getTimestamp()<\/td>\n<td style=\"width: 475.063px\">Gets the date and time as timestamp.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 129.063px\">setTimestamp($ts)<\/td>\n<td style=\"width: 475.063px\">Sets the date and time using timestamp $ts.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To get the current date and time, do not required passing any parameter. Following code snippet displays current date and time.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$dt = new DateTime();<\/p>\n<p>$disp = $dt-&gt;format(&#8216;d-m-Y h:m:s &#8216;);<\/p>\n<p>echo $disp;<\/p>\n<p>?&gt;<\/p>\n<p>22-11-2015 01:11:47 [Considering today is November 22,2015]<\/p>\n<p>The following code shows the use of setTime function.<\/p>\n<p>&lt;?php<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0$t = new DateTime();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo $t-&gt;format(&#8216;d-m-Y h:m:s&#8217;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$t-&gt;setTime(4,07,15);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt;&#8221;.$t-&gt;format(&#8216;d-m-Y g:i:s a&#8217;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>22-11-2015 03:11:14<\/p>\n<p>22-11-2015 4:07:15 am<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To modify the date and time modify function is used. Before using the modify function, clone the object and apply the modify function.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$invoicedate = new DateTime(&#8216;2014-03-23 14:30:18&#8217;); $due_date = clone $invoicedate;<\/p>\n<p>echo &#8220;&lt;dr&gt; Invoice date is:&#8221;.$invoicedate-&gt;format(&#8216;M j Y g:i:s a&#8217;); $due_date-&gt;modify(&#8216;+4 weeks&#8217;);<\/p>\n<p>echo &#8220;&lt;br&gt; Due Date (After 4 weeks of invoice date) is: &#8220;.$due_date-&gt;format(&#8216;M j Y \\a\\t g:i:s a&#8217;); ?&gt;<\/p>\n<p>Invoice date is:Mar 23 2014 2:30:18 pm<\/p>\n<p>Due Date (After 4 weeks of invoice date) is: Apr 20 2014 at 2:30:18 pm<\/p>\n<p>A timestamp can be converted to datetime object.<\/p>\n<p>&lt;?php<\/p>\n<p>$tomorrow = strtotime(&#8216;tomorrow 8am&#8217;);<\/p>\n<p>$nextday = new DateTime();<\/p>\n<p>$nextday-&gt;setTimestamp($tomorrow);<\/p>\n<p>echo $nextday-&gt;getTimestamp();<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>The output of the above code is:<\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0 1448262000<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>DateInterval Class<\/strong><\/p>\n<ul>\n<li>The date interval class is used to represent a span of time.<\/li>\n<li>The syntax to create dateinterval class object<\/li>\n<li>New DateInterval($str)<\/li>\n<li>The string $str must be start with P (period of time for Date)<\/li>\n<li>The string $str can take T to represent period of time.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Following table represents the parts of the interval string.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-177 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-94.png\" alt=\"\" width=\"649\" height=\"336\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-94.png 649w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-94-300x155.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-94-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-94-225x116.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-94-350x181.png 350w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/>\u00a0 \u00a0 Following code snippet using DateInterval object<br \/>\n$i1 = new DateInterval(&#8216;P30D&#8217;); \/\/ 30 Days Interval<br \/>\nprint_r($i1);<\/p>\n<p style=\"text-align: justify\">DateInterval Object ( [y] =&gt; 0 [m] =&gt; 0 [d] =&gt; 30 [h] =&gt; 0 [i] =&gt; 0 [s] =&gt; 0 [weekday] =&gt; 0 [weekday_behavior] =&gt; 0 [first_last_day_of] =&gt; 0 [invert] =&gt; 0 [days] =&gt; [special_type] =&gt; 0 [special_amount] =&gt; 0 [have_weekday_relative] =&gt; 0 [have_special_relative] =&gt; 0 )<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Below are few code which demonstrate the use of DateInterval<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>$i2 = new DateInterval(&#8216;P1Y23D&#8217;); \/\/ 1 Year 23 Days<\/p>\n<p>$i3 = new DateInterval(&#8216;PT1H2M12S&#8217;); \/\/ 1 Hour 2 Minutes 12 Second<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$i4 = new DateInterval(&#8216;P1Y23DT2H5M12S&#8217;); \/\/ 1 Year 23 Days 2 Hours 5 Minutes 12 Seconds<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To display the interval format function of the DateInterval object is used. The format method takes format parameter as string. It allows following format code.<\/p>\n<p>&nbsp;<\/p>\n<p>Syntax:<\/p>\n<p>&nbsp;<\/p>\n<p>Format($format)<\/p>\n<p>&nbsp;<\/p>\n<p>The codes for formatting are described below.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>Code<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>%R<\/td>\n<td>Sign of the interval + for positive interval value and \u2013 for negative interval value.<\/td>\n<\/tr>\n<tr>\n<td>%y<\/td>\n<td>Years<\/td>\n<\/tr>\n<tr>\n<td>%m<\/td>\n<td>Months<\/td>\n<\/tr>\n<tr>\n<td>%d<\/td>\n<td>Days<\/td>\n<\/tr>\n<tr>\n<td>%h<\/td>\n<td>Hours<\/td>\n<\/tr>\n<tr>\n<td>%i<\/td>\n<td>Minutes<\/td>\n<\/tr>\n<tr>\n<td>%s<\/td>\n<td>Seconds<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>For example,<\/p>\n<p>&nbsp;<\/p>\n<p>$i5 = new DateInterval(&#8216;P2M10D&#8217;);<\/p>\n<p>echo &#8220;&lt;br&gt;&#8221;.$i5-&gt;format(&#8216;%m months, %d days&#8217;);<\/p>\n<p>will print<\/p>\n<p>2 months, 10 days<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Use the DateInterval and DateTime classes together<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Following table shows 3 more methods of DateTime object which uses DateInterval objects.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><strong>Name<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>add($interval)<\/td>\n<td>Add the $interval time<\/td>\n<\/tr>\n<tr>\n<td>sub($interval)<\/td>\n<td>Subtract the $interval time<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">diff($date)<\/p>\n<p>&nbsp;<\/td>\n<td rowspan=\"2\">Returns a DateInterval object which represents the amount of time between the current<\/p>\n<p>Datetime object and $date.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The following code shows example for sub method.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$votingage = new DateInterval(&#8216;P18Y&#8217;);<\/p>\n<p>$dob = new DateTime();<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$dob-&gt;sub($votingage);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8216;&lt;br&gt; You can vote only if you were born on or before: &#8216;. $dob-&gt;format(&#8216;j\/n\/Y&#8217;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The output will be<\/p>\n<p>&nbsp;<\/p>\n<p>You can vote only if you were born on or before: 23\/11\/1997<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Numbers and Dates with PHP<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/ctHt4Xut3Gk\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>References:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional, Apress<\/p>\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass:Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O&#8217;Reilly Media<\/p>\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove: Professional PHP,Wrox<\/p>\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide, Pearson Education<\/p>\n<p style=\"text-align: justify\">11. http:\/\/www.php.net\/<\/p>\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>\n","protected":false},"author":3,"menu_order":15,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-hiren-joshi"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-173","chapter","type-chapter","status-publish","hentry","contributor-dr-hiren-joshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":12,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/173\/revisions"}],"predecessor-version":[{"id":496,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/173\/revisions\/496"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/173\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapter-type?post=173"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/contributor?post=173"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/license?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}