Asked  7 Months ago    Answers:  5   Viewed   38 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

62

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
Wednesday, March 31, 2021
 
millenomi
answered 7 Months ago
82

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");
Wednesday, March 31, 2021
 
Uours
answered 7 Months ago
95

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

Saturday, May 29, 2021
 
zIs
answered 5 Months ago
zIs
26

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);
Saturday, June 12, 2021
 
RompelStompel
answered 5 Months ago
89

You can use now library, it really simple :

now.BeginningOfMonth()    // 2013-11-01 00:00:00 Fri
now.EndOfMonth()          // 2013-11-30 23:59:59.999999999 Sat

Please take a look here for detail : https://github.com/jinzhu/now

Monday, September 6, 2021
 
Halsafar
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :