How do I get first day of previous month with start time? I am currently using this:
$date = new DateTime('first day of last month');
It gives me the first day of last month but with respect to my current time.
What I'm actually trying to do is get the start with respect to timezone too. For example:
$date = new DateTime('first day of previous month',
new DateTimeZone('UTC'));
Result would be July 1st, 2013 00:00:00. Or if I use:
$date = new DateTime('first day of previous month',
new DateTimeZone('Europe/Amsterdam'));
Expected result: June 30, 2013 21:00:00 (because of its offset).
How can I do this using PHP?
Just add time (time formats)
$date = new DateTime('first day of previous month 00:00:00', new DateTimeZone('UTC'));
var_dump($date->format('Y-m-d H:i:s')); // string(19) "2013-11-01 00:00:00"
$date = new DateTime('first day of previous month 00:00:00', new DateTimeZone('Europe/Amsterdam'));
var_dump($date->format('Y-m-d H:i:s')); // string(19) "2013-11-01 00:00:00"
Or add midnight (Relative Formats)
$date = new DateTime('midnight first day of previous month', new DateTimeZone('Europe/Amsterdam'));
var_dump($date->format('Y-m-d H:i:s'));
$date = new DateTime('midnight first day of previous month', new DateTimeZone('UTC'));
var_dump($date->format('Y-m-d H:i:s'));
Demo.
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
What I want to get are
today's 00:00:00
week start day's 00:00:00
month start day's 00:00:00
year start day's 00:00:00
So I write the code,
$today = new \DateTime();
$weekStart = new \DateTime();
$monthStart = new \DateTime();
$yearStart = new \DateTime();
$weekStart->modify('last sunday');
$monthStart->modify('first day of this months');
$yearStart->modify('first day of this year');
print $today->format('Y-m-d h:i:s')."\n";
print $weekStart->format('Y-m-d h:i:s')."\n";
print $monthStart->format('Y-m-d h:i:s')."\n";
print $yearStart->format('Y-m-d h:i:s')."\n";
It gave me the result:
2018-11-13 09:34:02
2018-11-11 12:00:00
2018-11-01 09:34:02
2018-11-01 09:34:02
I have two questions.
How can I make each DateTime 00:00:00??
How can I get the first day of year???
PHP's date parsing is somewhat tricky. Here's the code snippet which does what you want:
$today = new \DateTime('midnight');
$weekStart = new \DateTime('midnight last sunday');
$monthStart = new \DateTime('midnight first day of this month');
$yearStart = new \DateTime('midnight first day of january this year');
echo $today->format('Y-m-d H:i:s')."\n";
echo $weekStart->format('Y-m-d H:i:s')."\n";
echo $monthStart->format('Y-m-d H:i:s')."\n";
echo $yearStart->format('Y-m-d H:i:s')."\n";
DateTime value can be set upon construction; there's no need to call DateTime::modify().
Add midnight to get the time set to 00:00:00.
first day this year doesn't work in PHP, you need to use first day of january this year instead.
Reference: PHP Documentation of relative date formats.
you can try:
$date->setTime(0, 0, 0);
$date = new DateTime('2012-07-22');
echo $date->format('Y-m-d 00:00:00');
NO need to create a object of datetime(), use above code to get current date with time 00:00:00
I am trying to getting last friday date PST time, i am getting 15-11-2018,
Here is my code,
public function getPSTCurrentTime($time=null) {
$dateTime = new \DateTime($time, (new \DateTimeZone('UTC'))); // get current time as UTC/GMT timezone
$dateTime->setTimezone(new \DateTimeZone('PST')); // convert time as PST timezone
return $dateTime;
}
$date = new getPSTCurrentTime('last friday')->format('Y-m-d h:i:s');
The output i am getting was 15-11-2018, But i am expecting output was 16-11-2018
Try this,
$pst = new DateTimeZone('America/Los_Angeles');
$last_friday = new DateTime('last friday', $pst);
echo $last_friday->format('Y-m-d H:i:s'); // "2018-11-16 00:00:00"
After Comment:
To get the date based on the current timezone, then use date_default_timezone_get
$current_timezone = new DateTimeZone(date_default_timezone_get());
$last_friday = new DateTime('last friday', $current_timezone);
Output:
2018-11-16 00:00:00
I want to get 4 different dates with PHP: 1 day, 1 week, 1 month and 3 months.
Every date is from the current day, so lets say today is 2014-11-26.
get past dates based on current date.
$today = date("Y-m-d");
$one_day = $today - //how do I get yesterday from current day?
$one_week = $today - //how do I get one week from current day?
$one month = $today - //how do I get one month from current day?
$three_month = $today - //how do I get three month from current day?
The strtotime()-function is perfect for that approach.
Use something like
echo date( 'Y-m-d', strtotime("-1 day"));
echo date( 'Y-m-d', strtotime("-1 week"));
echo date( 'Y-m-d', strtotime("-1 month"));
echo date( 'Y-m-d', strtotime("-1 year"));
You can use the DateTime class:
$date = new DateTime('now');
echo $date->format('Y-m-d');
$date = new DateTime('-1 day');
echo $date->format('Y-m-d');
$date = new DateTime('-1 week');
echo $date->format('Y-m-d');
$date = new DateTime('-1 month');
echo $date->format('Y-m-d');
$date = new DateTime('-3 months');
echo $date->format('Y-m-d');
See Supported Date and Time Formats for more details.
I found a link earlier regarding using time diffs and getting the difference in minutes, hours and days:
How to get time difference in minutes in PHP
I was trying this:
$date1 = new DateTime('first day of this month', new DateTimeZone('Europe/Amsterdam'));
$date2 = new DateTime('first day of this month', new DateTimeZone('Europe/London'));
print_r($date1->format('Y-m-d H:i:s'));
print_r($date2->format('Y-m-d H:i:s'));
The output was like:
2013-12-01 13:00:36
2013-12-01 12:00:36
Then used this:
$diff = $date2->diff($date1);
print_r($diff);
But then i get 0 in all the differences. I want to get the difference between the two without using strtotime.. I is it outputing 0?
Your expectation doesn't make sense, since there is no difference. 2013-12-01 13:00:36 Amsterdam and 2013-12-01 12:00:36 London are the exact same point in time in human history. What you appear to expect is the offset difference between the London and Amsterdam timezones (i.e. GMT and GMT+1 differ by 1), but that has nothing to do with concrete timestamps.
You want to calculate the offset.Use DateTimeZone::getOffset()
$dateTimeZoneAmsterdam = new DateTimeZone("Europe/Amsterdam");
$dateTimeZoneLondon = new DateTimeZone("Europe/London");
$dateTimeAmsterdam = new DateTime('first day of this month', $dateTimeZoneAmsterdam);
$dateTimeLondon = new DateTime('first day of this month', $dateTimeZoneLondon);
$timeOffset = $dateTimeZoneAmsterdam->getOffset($dateTimeLondon);
print_r($timeOffset); // 3600
You are close. Try DateTime::diff