note 128580 added to function.strtotime

From: Date: Thu, 01 Jun 2023 08:29:02 +0000
Subject: note 128580 added to function.strtotime
Groups: php.notes 
Request: Send a blank email to php-notes+get-220536@lists.php.net to get a copy of this message
This is the link to the relative formats that can be used in this function. It's not easy to find in the documentation. https://www.php.net/manual/en/datetime.formats.relative.php Here some examples: <?php $today = date( 'Y-m-d', strtotime( 'today' ) ); echo "Today: " . $today; // Print: Today: 2023-06-01 //Basic Example $tomorrow = date( 'Y-m-d', strtotime( 'tomorrow' ) ); echo "\n\nTomorrow: " . $tomorrow; // Print: Tomorrow: 2023-06-02 $yesterday = date( 'Y-m-d', strtotime( 'yesterday' ) ); echo "\n\nYesterday: " . $yesterday; // Print: Yesterday: 2023-05-31 // Add o subtract Month $variable = date( 'Y-m-d', strtotime( '+1 month' ) ); echo "\n\nAdd one month: " . $variable; // Print: Add one month: 2023-07-01 $variable = date( 'Y-m-d', strtotime( '-1 month' ) ); echo "\n\nSubstract one month: " . $variable; // Print: Substract one month: 2023-05-01 // More complex formats: // ERROR / BUG: If today is the last day of the month. This examples can be one error or bug. // If you are looking el first day of previous or next month you dont must use last Relative Format. // You must use 'first day of 2 months ago' or 'first day of previous month' // See you: // https://bugs.php.net/bug.php?id=22486 // https://bugs.php.net/bug.php?id=44073 // strtotime("+1 Month") // produces the wrong result on the 31st day of the month. // on January 31, date("m",strtotime("+1 Month")) will return 03 instead of 02 // Another example: $dt1 = strtotime("2023-01-30"); $month = strtotime("+1 month", $dt1); echo "\n\nToday is: " . date( 'Y-m-d', $dt1 ); echo "\nBUG add month: " . date("Y-m-d", $month); // Print: // Today is: 2023-01-30 // BUG add month: 2023-03-02 $dt1 = strtotime("2023-02-28"); $month = strtotime("-1 month", $dt1); echo "\n\nToday is: " . date( 'Y-m-d', $dt1 ); echo "\nBUG substract month: " . date("Y-m-d", $month); // Print: // Today is: 2023-02-28 // BUG substract month: 2023-01-28 $dt1 = strtotime("2023-01-30"); $month = strtotime("first day of next month", $dt1); echo "\n\nToday is: " . date( 'Y-m-d', $dt1 ); echo "\nFirst day of next month: " . date("Y-m-d", $month); // Print: // Today is: 2023-01-30 // First day of next month: 2023-02-01 $dt1 = strtotime("2023-02-28"); $month = strtotime("last day of last month", $dt1); echo "\n\nToday is: " . date( 'Y-m-d', $dt1 ); echo "\nLast day of last month: " . date("Y-m-d", $month); // Print: // Today is: 2023-02-28 // Last day of last month: 2023-01-31 $dt1 = strtotime("2023-02-28"); $month = strtotime("first day of 2 months ago", $dt1); echo "\n\nToday is: " . date( 'Y-m-d', $dt1 ); echo "\nFirst day of 2 months ago: " . date("Y-m-d", $month); // Print: // Today is: 2023-02-28 // First day of 2 months ago: 2022-12-01 ?> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.13.130 (proxied: 81.61.178.100) ---- Manual Page -- https://php.net/manual/en/function.strtotime.php Edit -- https://main.php.net/note/edit/128580 Del: integrated -- https://main.php.net/note/delete/128580/integrated Del: useless -- https://main.php.net/note/delete/128580/useless Del: bad code -- https://main.php.net/note/delete/128580/bad+code Del: spam -- https://main.php.net/note/delete/128580/spam Del: non-english -- https://main.php.net/note/delete/128580/non-english Del: in docs -- https://main.php.net/note/delete/128580/in+docs Del: other reasons-- https://main.php.net/note/delete/128580 Reject -- https://main.php.net/note/reject/128580 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220536) next »