MOHAMMED MOHAMMED - 16 days ago 5
PHP Question

fetching only year and month from a date saved in the format of strtotime

My date string value is

1478025000
. want to get year and month in the string format itself from this string,is it possible? the day should be 0 and next month and next year like that.
my desired output should be
1477852200

Answer

Try something like this:

$epoch = 1478025000;
$dt = new DateTime("@$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m'); // Display as year and month: YYYY-MM
echo $dt->format('m-Y'); // Display as month and year: MM-YYYY

If you want to get just the year and month as an epoch, try something like:

$epoch = 1478025000;
$dt = new DateTime("@$epoch"); // convert UNIX timestamp to PHP DateTime
$year = $dt->format('Y');
$day = $dt->format('n');
$answer = new DateTime();
$answer->setDate($year, $month, 0);
$answer->setTime(0, 0, 0);
echo $answer->getTimestamp();