Introduction
In this video, I’m going to share how to develop a calendar starting with Monday.
(If you want to create Sunday start calendar, please check this post.)
I think coding a calendar is the best way to get used to Date function.
In this video, you’ll use several date formats like N, j, F, t…
But you don’t need to remember all of them. (I still forget what N stands for!)
Just know how to use them 🙂
Hope you enjoy coding!
Environment
MAMP / PHP version | 7.1.19 (Required PHP 5.4 or later) |
Text Editor | Coda2 |
Browser | Google Chrome |
Video
A Calendar with PHP (Starting with Monday)
Tips & Memo
Date Format
date($format, $timestamp);
$timestamp is optional and defaults to the current time.
Format | Returned Value | |
---|---|---|
Year | Y | 2018 |
Month | F | January ~ December |
m | 01 ~ 12 | |
Day | j | 1 ~ 31 |
N | 1:Mon 2:Tue … 7:Sun | |
t | 28 ~ 31 |
Strtotime
strtotime($time_string, $timestamp);
$timestamp is optional and defaults to the current time.
Example:
date(‘Y-m-d’, strtotime(‘-1 month’));
date(‘Y-m-d’, strtotime(‘+3 week’));
date(‘Y-m-d’, strtotime(‘+2 day’));
date(‘Y-m-d’, strtotime(‘last Monday’));
Be careful of the base timestamp.
Example: Get the date one month before July.
echo date(‘Y-m-d’, strtotime(‘2018-07-01 -1 month’));
→ 2018-06-01
echo date(‘Y-m-d’, strtotime(‘2018-07-31 -1 month’));
→ 2018-07-01
Sample Code