7

Need to find the timestamp for the first minute of the first day of the current week.

What is the best way to do this?

<?php

$ts = mktime(); // this is current timestamp

?>
acoder
  • 513
  • 2
  • 10
  • 20

6 Answers6

18

If Monday is your first day:

$ts = mktime(0, 0, 0, date("n"), date("j") - date("N") + 1);
Tchoupi
  • 14,560
  • 5
  • 37
  • 71
  • 2
    And just get rid of the 1 if you want Sunday: `$ts = mktime(0, 0, 0, date("n"), date("j") - date("N"));` – benesch Mar 23 '12 at 17:55
  • Thank you, but perhaps this will not work if the Monday is in previous month or previous year. – acoder Mar 23 '12 at 19:02
  • 1
    @acoder Good point! BUT... I just tested it, and PHP seems to handle perfectly well the negative number resulting from `date("j") - date("N") + 1` if the current date is, for example, March 3rd. – Tchoupi Mar 23 '12 at 19:21
  • @Mathieu This is a nice and valuable notice, really! This solution can be used in PHP5, as date("N") has been added in PHP 5.1.0 according to php documentation. – acoder Mar 23 '12 at 19:46
  • ... and if we change `date("N")` to `date("w")` then it should work in PHP4 as well i suppose. – acoder Mar 24 '12 at 08:07
7

If you think Monday is the first day of the current week...

$ts = strtotime('Last Monday', time());

If you think Sunday is the first day of the current week...

$ts = strtotime('Last Sunday', time());
Ryan Kempt
  • 4,200
  • 6
  • 30
  • 41
  • 1
    This doesn't seem to work when today is the day first day of the week. For instance, if we are Monday, `strtotime('Last Monday')` will return Monday a week ago, not the start of the current week (today at midnight) – BeetleJuice Jun 25 '16 at 09:47
5

If it is the monday you're looking for:

$monday = new DateTime('this monday');
echo $monday->format('Y/m/d');

If it is the sunday:

new DateTime('this sunday'); // or 'last sunday'

For further information about these relative formats, look here "PHP: Relative Formats"

dan-lee
  • 14,365
  • 5
  • 52
  • 77
2

First of all, date/time functions in PHP are really slow. So I try to call them a little as possible. You can accomplish this using the getdate() function.

Here's a flexible solution:

/**
 * Gets the timestamp of the beginning of the week.
 *
 * @param integer $time           A UNIX timestamp within the week in question;
 *                                defaults to now.
 * @param integer $firstDayOfWeek The day that you consider to be the first day
 *                                of the week, 0 (for Sunday) through 6 (for
 *                                Saturday); default: 0.
 *
 * @return integer A UNIX timestamp representing the beginning of the week.
 */
function beginningOfWeek($time=null, $firstDayOfWeek=0)
{
    if ($time === null) {
        $date = getdate();
    } else {
        $date = getdate($time);
    }

    return $date[0]
        - ($date['wday'] * 86400)
        + ($firstDayOfWeek * 86400)
        - ($date['hours'] * 3600)
        - ($date['minutes'] * 60)
        - $date['seconds'];

}//end beginningOfWeek()
jnrbsn
  • 2,498
  • 1
  • 18
  • 25
  • 1
    Awesome solution. Straight to the point. Works in PHP4 as well. Thank You! – acoder Mar 23 '12 at 19:33
  • This solution is not time zone aware, is it? – ckruse Mar 23 '12 at 19:44
  • 1
    It uses whatever the default timezone is for your server/script. – jnrbsn Mar 23 '12 at 19:48
  • Like gmmktime() gets Unix timestamp for a GMT date, does getdate() have an equivalent for GMT dates? – acoder Mar 23 '12 at 20:10
  • There's not one built-in, but there are lots of [examples in the comments on the PHP Manual page](http://www.php.net/manual/en/function.getdate.php). – jnrbsn Mar 23 '12 at 20:21
  • @acoder You could make the function timezone aware by adding an optional `$tzOffset=null` parameter and then adding something like `+ ($tzOffset === null ? 0 : (int)date('Z') + $tzOffset)` to the end of the return statement. – jnrbsn Mar 23 '12 at 20:41
  • This procedural approach is not the right way in my opinion. If you really care about micro optimization, okay. But today all servers can handle such calls (e.g. DateTime) in micro seconds. You always should consider PHP5 and OOP. Otherwise you are just stuck in the past. Just my 2 cents – dan-lee Mar 23 '12 at 22:49
  • Thanks for the idea & code. I decide to use this one after I saw all other ideas. I can calculate first day of this month easily with this idea too. Perfect for me. – Joe Huang May 22 '14 at 02:08
0

Use this to get timestamp of which weekday you want, instead 'Saturday' write first day of the week:

strtotime('Last Saturday',mktime(0,0,0, date('m'), date('d')+1, date('y')))

for example: in above code you get timestamp of last saturday, instead of this week's saturday.

note that, if the weekday is Saturday now, this will return today time stamp.

Foad Tahmasebi
  • 1,333
  • 4
  • 16
  • 33
0

I use the following snippet of code:

public static function getTimesWeek($timestamp) {

  $infos = getdate($timestamp);
  $infos["wday"] -= 1;
  if($infos["wday"] == -1) {
    $infos["wday"] = 6;
  }

  return mktime(0, 0, 0, $infos["mon"], $infos["mday"] - $infos["wday"], $infos["year"]);
}
ckruse
  • 9,642
  • 1
  • 25
  • 25