What is the best way to find out if there are 2 dates in one month in PHP?

I have two dates that are unix timestamps, and I would need to find out if the dates match the same month. For example, if I have 1393624800 = 02 / 28 / 14and 1391472000 = 2 / 4 / 2014. I decided that I could convert the unix timestamps to a format, for example 28-02-2014, and extract the month, but I feel like a kind of dirty method. What is the best way to do this?

EDIT: Sry, I forgot. You also need to make sure that same year.

+4
source share
5 answers

I would suggest using the DateTime class

$date1 = new DateTime();
$date1->setTimestamp($timestamp1);

$date2 = new DateTime();
$date2->setTimestamp($timestamp2);

if ($date1->format('Y-m') === $date2->format('Y-m')) {
    // year and month match
}

, PHP, DateTime . .

+12
if (date("F Y", $date1) == date("F Y", $date2))

http://au2.php.net/manual/en/function.date.php

, - :

if (date("n", $date1) == date("n", $date2))
+4

, :

$first = date("m", $timestamp);
$second = date("m", $timestamp2);

if($first == $second) {
   // they are in same month
}
else {
   // they aren't
}
+3

unix, php.

, $date1 = 1393624800;

& $date2 = 1391472000;

, ('m')

PHP CODE

if(date('m', $date1) == date('m', $date2)){ //Action; }

, :)

0

$time = strtotime(date('Y-m-01 00:00:00')); // == 1338534000

:

$date = date('Y-m-d H:i:s', $time); // == 2012-06-01 00:00:00

PHP date() time().

-1

Source: https://habr.com/ru/post/1525166/


All Articles