PHP and MySQL syntax: Difference between revisions

Jump to navigation Jump to search
Line 128: Line 128:
==== Previous month based on current month ====
==== Previous month based on current month ====
* PHP: {{kbd | key = <nowiki>echo date("Y-m", strtotime("-1 month"));</nowiki>}} // Returns YYYY-MM <ref>[https://stackoverflow.com/questions/1889758/getting-last-months-date-in-php Getting last month's date in php - Stack Overflow]</ref>
* PHP: {{kbd | key = <nowiki>echo date("Y-m", strtotime("-1 month"));</nowiki>}} // Returns YYYY-MM <ref>[https://stackoverflow.com/questions/1889758/getting-last-months-date-in-php Getting last month's date in php - Stack Overflow]</ref>
* MySQL:  Using [https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format DATE_FORMAT] & [https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-add DATE_ADD] functions. {{kbd | key = <nowiki>SELECT DATE_FORMAT(NOW() - INTERVAL 1 MONTH, "%Y-%m");</nowiki>}} // Returns YYYY-MM <ref>[https://stackoverflow.com/questions/22953258/mysql-first-day-and-last-day-of-current-and-previous-month-from-date-no-timesta php - MySQL first day and last day of current and previous month from date (no timestamp) - Stack Overflow]</ref>
* MySQL:   
** Returns YYYY-MM: Using [https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format DATE_FORMAT] & [https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-add DATE_ADD] functions. {{kbd | key = <nowiki>SELECT DATE_FORMAT(NOW() - INTERVAL 1 MONTH, "%Y-%m");</nowiki>}} <ref>[https://stackoverflow.com/questions/22953258/mysql-first-day-and-last-day-of-current-and-previous-month-from-date-no-timesta php - MySQL first day and last day of current and previous month from date (no timestamp) - Stack Overflow]</ref>
** Returns YYYY-MM-DD: {{kbd | key = <nowiki>SELECT DATE(NOW() - INTERVAL 1 MONTH);</nowiki>}}
* Excel: Using [https://support.office.com/zh-tw/article/EOMONTH-%E5%87%BD%E6%95%B8-7314ffa1-2bc9-4005-9d66-f49db127d628 EOMONTH()] function. {{kbd | key = <nowiki>=YEAR(EOMONTH(NOW(), -2)+1)&"-"&TEXT(MONTH(EOMONTH(NOW(), -2)+1), "00")</nowiki>}} // Returns YYYY-MM <ref>[https://exceljet.net/formula/get-first-day-of-previous-month Excel formula: Get first day of previous month | Exceljet]</ref>
* Excel: Using [https://support.office.com/zh-tw/article/EOMONTH-%E5%87%BD%E6%95%B8-7314ffa1-2bc9-4005-9d66-f49db127d628 EOMONTH()] function. {{kbd | key = <nowiki>=YEAR(EOMONTH(NOW(), -2)+1)&"-"&TEXT(MONTH(EOMONTH(NOW(), -2)+1), "00")</nowiki>}} // Returns YYYY-MM <ref>[https://exceljet.net/formula/get-first-day-of-previous-month Excel formula: Get first day of previous month | Exceljet]</ref>


Navigation menu