How To Become A Programmer Without A Degree & Experience

A Calendar with PHP (starting with Monday)

A Calendar with PHP (Starting with Monday)

 
 
 
* Starting with Sunday version
How to code calendar in PHP  
 

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 version7.1.19 (Required PHP 5.4 or later)
Text EditorCoda2
BrowserGoogle Chrome

 
 

Tips & Memo

Date Format

date($format, $timestamp);
$timestamp is optional and defaults to the current time.

FormatReturned Value
YearY2018
MonthFJanuary ~ December
m01 ~ 12
Dayj1 ~ 31
N1:Mon 2:Tue … 7:Sun
t28 ~ 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