Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - how to use $timestamp to check if today is Monday or 1st of the month?

Tags:

php

timestamp

I have been looking through examples online, and I am finding them a bit cryptic or overkill.

What I need to do is something like this:

$timestamp = time();

and then find out if the day is a Monday or a fist of the month?

I am sure it is possible, I am just not sure how to do that.

like image 904
Genadinik Avatar asked Dec 28 '12 01:12

Genadinik


4 Answers

Actually, you don't need timestamp variable because:

Exerpt from date function of php.net:

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().

if(date('j', $timestamp) === '1') 
    echo "It is the first day of the month today\n";

if(date('D', $timestamp) === 'Mon') 
    echo "It is Monday today\n";
like image 145
Roman Newaza Avatar answered Oct 15 '22 21:10

Roman Newaza


This should solve it:

$day = date('D');
$date = date('d')
if($day == Mon){
    //Code for monday
}
if($date == 01){
    //code for 1st fo the month
}
else{
    //not the first, no money for you =/
}
like image 33
DalekSall Avatar answered Oct 15 '22 21:10

DalekSall


This will grab.. Monday from mysql

$monday = 1; //tuesday= 2.. sunday = 7

    AND $monday = (date_format(from_unixtime(your_date_column),'%w')) 

OR days..

$day = 1; ///1st in month

    AND $day = (date_format(from_unixtime(your_date_column),'%d')) 

JUST TO KNOW

$date  = date("d"); //1st?
$dayinweek = date("w"); //monday? //as a number in a week what you need more then just "Monday" I guess..
like image 4
Xfile Avatar answered Oct 15 '22 22:10

Xfile


You can use: strtotime

$firstdaymonth = strtotime('first day this month');
like image 1
Phillip Plum Avatar answered Oct 15 '22 21:10

Phillip Plum