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

Related searches

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.

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";

PHP, This page describes the different relative date/time formats that the strtotime(), DateTime and date_create() 'first day of', Sets the day of the first of the current month. reltext space 'week', Handles the special format "weekday + last/this/ next week". If the ordinal being used is "last" and the next month is n, use the "n 1". A program to check if a binary tree is BST or not in C ? How to check if an iOS program is in foreground or in background? How to check if an URL is valid or not using Java? How to check if a given character is a number/letter in Java? How to check Leap Year in Java 8 How to get current timestamp in Java 8? How to check if two dates are equal

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 =/
}

Relative Formats - Manual, The Unix timestamp that this function returns does not contain information about time zones. given weekday in a month where that weekday was the first day of the month Use \"first tuesday $thisMonth $thisYear\" to check for first Tuesday. The optional timestamp parameter in the date() function specifies a timestamp. If omitted, the current date and time will be used (as in the examples above). The PHP mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

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..

strtotime - Manual, The optional timestamp parameter is an integer Unix timestamp that defaults It is possible to use date() and mktime() together to find dates in the future or the past. $today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month be skipped (e.g. "skip July 4th in any year, skip the first Monday in September in any year"). In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.

You can use: strtotime

$firstdaymonth = strtotime('first day this month');

date - Manual, The loop starts at the first timestamp, then incremented by adding 86400 seconds (ie. wanting to know the date/time that corresponds to a given unix timestamp, the system clock; also, if today is between Sunday and the end of the month,� The accepted answer works only if you want exactly 31 days later. That means if you are using the date "2013-05-31" that you expect to not be in June which is not what I wanted.

Because $date can monday or sunday. Should be check it

public function getWeek($date){
    $date_stamp = strtotime(date('Y-m-d', strtotime($date)));

     //check date is sunday or monday
    $stamp = date('l', $date_stamp);      

    if($stamp == 'Mon'){
        $week_start = $date;
    }else{
        $week_start = date('Y-m-d', strtotime('Last Monday', $date_stamp));
    }


    if($stamp == 'Sunday'){
        $week_end = $date;
    }else{
        $week_end = date('Y-m-d', strtotime('Next Sunday', $date_stamp));
    }        
    return array($week_start, $week_end);
}

Date/Time Functions - Manual, You can use the date function. I'm using strtotime to get the timestamp to that day ; there are other solutions, like mktime , for instance. For instance, with the 'D' modifier, for the textual representation in three letters :

Unix Time. Unix time (also known as POSIX time or Epoch time) is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds.

Returns a string formatted according format using the given timestamp. Month and weekday names and other language-dependent strings respect the current locale set with setlocale() PHP Version: 4+ PHP Changelog: PHP 5.1: Now issues E_STRICT and E_NOTICE time zone errors

PHP 5.2.7: In earlier versions, if requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month it would incorrectly add one week to the returned timestamp. This has been corrected now PHP 5.1.0: Returns FALSE on failure (earlier versions returns -1), and issues E_STRICT and E_NOTICE time zone errors

Comments
  • php.net/manual/en/class.datetime.php
  • you nedd to query data according to Monday or 1st or just to find out is it 1st or Monday? ;)
  • @Xfile I just need to know if today is monday? And in a seperate line I need to know if today is first of the month. Essentially there are reminders that need to be sent weekly and monthly so that is why I need those.
  • if(date('w', $timestamp) === '1') is better.
  • why is it better? explain. 'D' format character is more human readable.
  • I think in certain locale the result of date('D') can be Monday but not read as 'Mon'. moreover, your code don't really care if it's human readable, it wants to react the correct way if it's monday or not.
  • sorry, little bit confused, how can I enter 'first day this month' into that function?
  • by writing literally 'first day this month'. strtotime accepts those. google it :)