echo mydate(strtotime('1 am first day of this month'));
Above works with result 2017-10-01 01:00:00, but I have difficulty to do it for 0 am. Neither 24am, 24pm, 0pm, first second works.
Use midnight:
echo mydate(strtotime('midnight first day of this month'));
Demo
Better to use DateTime for date and time manipulations,
$date = new DateTime('midnight first day of this month');
echo $date->format('Y-m-d H:i:s');
or if there is need to UnixTimestamp : $date->format('U');
or echo $date->getTimestamp();
Related
I'm using PHP's DateTime object to manipulate dates. I found a strange case where the result date seems wrong:
$dt = new DateTime('2020-02-29 23:59:59');
$dt->modify('-1 year');
echo $dt->format('Y-m-d H:i:s');
This code gives 2019-03-01 23:59:59 instead of 2019-02-28 23:59:59. I believe it is due to 2020 having a Feb. 29th yet I have no clue how to fix this issue.
Note that the modifier can be anything, not just -1 year. A solution that would isolate the year number minus one wouldn't fit my needs.
I figured out myself a work around that seems to work.
I noted that things work well with first day of the month:
$dt = new DateTime('2020-02-01 00:00:01');
$dt->modify('-1 year');
echo $dt->format('Y-m-d H:i:s'); // 2019-02-01 00:00:01
From this statement, I just added a few steps when I need to manipulate "end of the month" dates:
$dt = new DateTime('2020-02-29 23:59:59');
$dt->modify('first day of this month');
$dt->modify('-1 year');
$dt->modify('last day of this month');
echo $dt->format('Y-m-d H:i:s'); // 2020-02-28 23:59:59
If i have for example :
'2020-01-01'
I want to have two dates :
-The last day of the prévious month :
'2019-12-31'
-The first day of next month :
'2020-02-01'
I tried to use something like echo date("Y-n-31", strtotime('2020-01-01')); but i don't know.
Thank you.
Use below code:
<?php
$month_end = new DateTime("last day of last month");
$month_ini = new DateTime("first day of next month");
echo $month_end->format('Y-m-d'); // will print, Last day of last month
echo $month_ini->format('Y-m-d'); // will print First day of next month
?>
With Datetime object with user defined date (Custom date)
# with datetime object
$d1 = new DateTime('2019-12-01');
$d1 -> modify('last day of last month');
echo $d1 -> format('d.m.Y'), "\n";
$d2 = new DateTime('2019-12-01');
$d2 -> modify('first day of next month');
echo $d2 -> format('d.m.Y'), "\n";
Output:
30.11.2019
01.01.2020
Try this, Use strtotime() for add month or minus month
$date='2020-01-01';
echo date("Y-m-t", strtotime('-1 month'.$date)); //2019-12-31
echo date("Y-m-01", strtotime('+1 month'.$date)); //2020-02-01
strtotime()
this php function convert english textual date-time discription into UNIX timestamp
the first step converts textual date-time into a timestamp
now you have timestamp then use the date() function
-The last day of the prévious month :
echo date("Y-m-d", strtotime("Last day of last month"));
-The first day of next month :
echo date("Y-m-d", strtotime("First day of next month"));
You can use DateTime's method modify() to achieve relative dates:
<?php
$dateOld = new \DateTimeImmutable('2020-01-01');
echo $dateOld->modify("last day of last month")->format('Y-m-d');
echo PHP_EOL;
echo $dateOld->modify("first day of next month")->format('Y-m-d');
Try it here: https://3v4l.org/qALa5
$date='2020-01-01';
echo date("Y-m-d", 'last day of previous month', strtotime($date)));
echo date("Y-m-d", 'first day of next month', strtotime($date)));
I just hope this question won't be marked as a duplicate because I've seen similar questions on stackoverflow but they all talk about adding days to the date, The problem here is that i want to add some particular months to a particular date which is gotten from my database I've tried adding it using strtotime() but the date just returns 1st January 1970, the code looks like this
<?php echo date('jS F Y', strtotime("$date +1 month")); ?>
//This is the value of date
$date = $student->date;
How to I add months to this particular date? Please note that the date is a timestamp in my database.Thanks
You have a Unix timestamp, not an actual date. Here I use the DateTime class to create a datetime object using that Unix timestamp. Then I can add a month to it and format the output.
$date = new DateTime('#'.$student->date);
$date->modify('+1 month');
echo $date-format('jS F Y');
If you want to stick to using date() and strtotime() you would use this:
echo date("jS F Y", strtotime("+1 month", $student->date));
strtotime() would take the starting date as the second parameter and then how you wish to modify it as your first parameter.
You should check out the documentation here,
But the just of it is the $date->add function. It allows you to add any amount of time to a timestamp using a DateInterval. Its a little tricky to get used to but here are a couple of examples:
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
which outputs:
2000-01-01 10:00:30
2007-06-05 04:03:02
The date interval is formatted in years months days hours minuets seconds, simply put in the amount you want and it will add it, so in your case:
<?php
$date->add(new DateInterval('P1M'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
PHP's strtotime() function allows for a second parameter that allows you to set a relative date.
If you would like to add a month to tomorrow, here's how:
<?php
echo date("jS F Y", strtotime("+1 month", strtotime("2014-10-09")));
// returns: 9th November 2014
im looking for the DateTime modify String for the first day of the year (now 1. January 2011). I tried the following:
<?php
$time = new DateTime();
// works as expected, the first day of the current month
$time->modify('first day of this month');
echo $time->format('c')."\n";
// this doesn't work. I also tried several other ways
$time->modify('first day of january');
echo $time->format('c')."\n";
>
I know there are other ways to retrieve the date, but I search an string for DateTime->modify() no other solution.
You should specify the year too, as you can see in this example:
"first day of January 2008"
from the official doc.
Update: It works on php version >= 5.3.6
On v5.5.6
echo date('Y-m-d', strtotime('first day of January this year'));
Result: 2013-01-01
To get the day of the week for the first of the year
or the first day of the month
<?php
//This is for a given month
$m="May";
// this id for this month
//$m=date('F');
//if you want the day of Sunday instead D use lower case l
echo date('D', strtotime('first day of January this year'));
echo "<br>". date("D", strtotime('first day of'. $m ));
?>
Result Wed For May with D
Result Wednesday with l
This work for me (PHP 5.6 - not tested on older version)... as we talk for DateTime object
//Get current datetime
$now = new DateTime();
$now->modify('first day of January this year');
echo $now->format('Y-m-d');
// Print (current year)-01-01
echo (new DateTime())->modify('first day of January this year')->format('Y-m-d');
Given a time, how can I find the time one month ago.
strtotime( '-1 month', $timestamp );
http://php.net/manual/en/function.strtotime.php
In php you can use strtotime("-1 month"). Check out the documentation here: http://ca3.php.net/strtotime
We can achieve same by using PHP's modern date handling. This will require PHP 5.2 or better.
// say its "2015-11-17 03:27:22"
$dtTm = new DateTime('-1 MONTH', new DateTimeZone('America/Los_Angeles')); // first argument uses strtotime parsing
echo $dtTm->format('Y-m-d H:i:s'); // "2015-10-17 03:27:22"
Hope this adds some more info for this question.
<?php
$date = new DateTime("18-July-2008 16:30:30");
echo $date->format("d-m-Y H:i:s").'<br />';
date_sub($date, new DateInterval("P1M"));
echo '<br />'.$date->format("d-m-Y").' : 1 Month';
?>
PHP 5.2=<
$date = new DateTime(); // Return Datetime object for current time
$date->modify('-1 month'); // Modify to deduct a month (Also can use '+1 day', '-2 day', ..etc)
echo $date->format('Y-m-d'); // To set the format
Ref: http://php.net/manual/en/datetime.modify.php
This code is for getting 1 month before not 30 days
$date = "2016-03-31";
$days = date("t", strtotime($date));
echo date("Y-m-d", strtotime( "-$days days", strtotime($date) ));
These answers were driving me nuts. You can't subtract 31 days and have a sane result without skipping short months.
I'm presuming you only care about the month, not the day of the month, for a case like filtering/grouping things by year and month.
I do something like this:
$current_ym = date('ym',strtotime("-15 days",$ts));