Asked  2 Years ago    Answers:  5   Viewed   78 times

I'm looking for the best way to get the first and the last day of a last month. I use them for make SQL queries to get stats of last month.

I think that this is the best way, more optimized but not more comprensive, anyone have another way to do the same?

    $month_ini = date("Y-m-d", mktime(0, 0, 0, date("m", strtotime("-1 month")), 1, date("Y", strtotime("-1 month"))));

    $month_end = date("Y-m-d", mktime(0, 0, 0, date("m", strtotime("-1 month")), date("t", strtotime("-1 month")), date("Y", strtotime("-1 month"))));

Thanks!!

 Answers

1

In PHP 5.3, you can use the DateTime class :

<?php

$month_ini = new DateTime("first day of last month");
$month_end = new DateTime("last day of last month");

echo $month_ini->format('Y-m-d'); // 2012-02-01
echo $month_end->format('Y-m-d'); // 2012-02-29
Tuesday, October 18, 2022
 
sivar
 
2

Use H instead:

$test = 'Fri, 15 Jan 2016 15:14:10 +0800';
$t = date('Y-m-d H:i:s',strtotime($test));
echo $t;

H: 24-hour format of an hour with leading zeros 00 through 23

G should be the same, but without leading zeroes though. I suspect that your PHP is set to a different timezone than +0800. Can you confirm your timezone (date_default_timezone_get())?

EDIT

OP confirmed that his timezone was set to UTC, in which case it maskes perfect sense that it shows 7 in the morning, as date uses PHPs default timezone.

If you want to "inherit" the Timezone, while getting more flexibility, you should switch to DateTime:

echo (new DateTime($test))->format("Y-m-d H:i:s");
Sunday, July 31, 2022
 
joga
 
3

You need to use something like mysqli or pdo to execute your query and extract the data. Here's the general idea, using mysqli:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$dates_sql = "SELECT UNIX_TIMESTAMP(datetime) AS tstamp FROM employee_datetable LIMIT 7";
$result = $mysqli->query($dates_sql);

while($row = $result->fetch_array()) {
    $FormattedPhpDate = date('M d, Y', $row['tstamp']);
    echo "Date: " . $FormattedPhpDate . "<br/>";
}

Note: Since you are using UNIX_TIMESTAMP in your query there is no need to convert the date to a timestamp using strtotime()


For more info see documentation for mysqli, specifically mysqli_query and mysqli_fetch_array

Wednesday, August 17, 2022
5

DateTime structure stores only one value, not range of values. MinValue and MaxValue are static fields, which hold range of possible values for instances of DateTime structure. These fields are static and do not relate to particular instance of DateTime. They relate to DateTime type itself.

Suggested reading: static (C# Reference)

UPDATE: Getting month range:

DateTime date = ...
var firstDayOfMonth = new DateTime(date.Year, date.Month, 1);
var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
Thursday, November 17, 2022
 
stefon
 
1

How about defining functions FIRST_WDOM() (week/work day of month) and LAST_WDOM() in the style of LAST_DAY()?

DELIMITER //
CREATE FUNCTION first_wdom (d DATETIME) -- First work/week day of month
RETURNS DATE DETERMINISTIC
BEGIN
  DECLARE first_dom DATE;
  SET first_dom = DATE_FORMAT(d, '%Y-%m-01');

  RETURN first_dom + INTERVAL (CASE DAYOFWEEK(first_dom)
                               WHEN 1 THEN 1
                               WHEN 7 THEN 2
                               ELSE 0
                               END) DAY;
END //
CREATE FUNCTION last_wdom (d DATETIME) -- Last work/week day of month
RETURNS DATE DETERMINISTIC
BEGIN
  DECLARE last_dom DATE;
  SET last_dom = LAST_DAY(d);

  RETURN last_dom - INTERVAL (CASE DAYOFWEEK(last_dom)
                              WHEN 1 THEN 2
                              WHEN 7 THEN 1
                              ELSE 0
                              END) DAY;
END //
DELIMITER ;

mysql> SELECT FIRST_WDOM('2011-05-10'), LAST_WDOM('2011-05-10');
+--------------------------+-------------------------+
| FIRST_WDOM('2011-05-10') | LAST_WDOM('2011-05-10') |
+--------------------------+-------------------------+
| 2011-05-02               | 2011-05-31              | 
+--------------------------+-------------------------+
1 row in set (0.01 sec)

This is merely an adaptation of ic3b3rg's answer, and assumes that Saturday and Sunday are your weekend days. I use DATETIME, rather than DATE, as the input type to avoid truncation warnings when passing in, say, NOW().

Wednesday, August 3, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages