Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
PHP - How to use $timestamp to check if today is Monday or 1st of the month?
In PHP, you can use the date() function with a timestamp to check if today is Monday or the 1st of the month. The date() function formats a timestamp based on the specified format string.
The timestamp parameter is optional and defaults to the current time if not provided.
Syntax
date(format, timestamp)
Parameters
- format − Required. Specifies the format of the outputted date string
- timestamp − Optional. Specifies a Unix timestamp. Default is the current date and time
Example
Here's how to check if a given timestamp represents Monday or the 1st day of the month ?
<?php
// Example timestamp for demonstration (January 1st, 2024 - Monday)
$timestamp = mktime(0, 0, 0, 1, 1, 2024);
// Check if it's the first day of the month
if(date('j', $timestamp) === '1') {
echo "It is the first day of the month<br>";
}
// Check if it's Monday
if(date('D', $timestamp) === 'Mon') {
echo "It is Monday<br>";
}
// Display the actual date for reference
echo "Date: " . date('Y-m-d (D)', $timestamp) . "<br>";
?>
The output of the above code is ?
It is the first day of the month It is Monday Date: 2024-01-01 (Mon)
Format Characters
| Format | Description | Example |
|---|---|---|
| j | Day of the month without leading zeros | 1 to 31 |
| D | A textual representation of a day (3 letters) | Mon, Tue, Wed |
| N | ISO-8601 numeric representation of the day | 1 (Monday) to 7 (Sunday) |
Using Current Time
To check the current date without specifying a timestamp ?
<?php
// Check current date (no timestamp parameter)
if(date('j') === '1') {
echo "Today is the first day of the month<br>";
} else {
echo "Today is not the first day of the month<br>";
}
if(date('D') === 'Mon') {
echo "Today is Monday<br>";
} else {
echo "Today is not Monday<br>";
}
echo "Current date: " . date('Y-m-d (D)') . "<br>";
?>
Conclusion
Use date('j', $timestamp) to check if it's the 1st day of the month and date('D', $timestamp) to check if it's Monday. Omit the timestamp parameter to check the current date.
Advertisements
