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
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
I am working on my php script to set up the date with the time. I need some help with convert the day date to the current day and next day date, example: my current time is 15:27 and my current date is 27-11-2019 so when I have the string for the variable get_time1 is 06:00:00, I want to convert it to 28-11-2019 06:00:00. When I have the variable get_time2 that have the time which it is 23:00:00 as my current time is before 23:00:00 so i want to convert the date with the current date with the time to 27-11-2019 23:00:00.
Code:
<?php
$get_time1 = '06:00:00';
$get_time2 = '23:00:00';
date_default_timezone_set('Europe/London');
$Date = date('Y-m-d');
$time = date('H:i:s');
?>
Can you please show me an example how I can set up the day date with the time 06:00:00 and 23:00:00 as if the time 06:00:00 is after 12am to set up the next day date and if the time 23:00:00 is before 12am then set up the time with the current date?
Thank you.
This just creates a DateTime object from the time (which will default it to todays date) and if this is less than the current date and time, it adds 1 day...
$date = DateTime::createFromFormat("H:i:s", $get_time2);
if ( $date < new DateTime() ) {
$date->modify("+1 day");
}
which gives
2019-11-27 23:00:00
and for $get_time1...
2019-11-28 06:00:00
If you use a DateTime that will allow you to do date arithmetic.
$now = new DateTime();
$tomorrow = $now->modify("+1 day");
You can also use strtotime to get a unix timestamp as explained in this answer.
$tomorrow = strtotime('+1 day');
maybe this will do?
$offset = timezone_offset_get( timezone_open( "Europe/London" ), new \DateTime() );
echo 'in London' . gmdate('d-m-Y H:i:s', date( "U" )+$offset);
echo 'current location: ' . date('d-m-Y H:i:s', date( "U" ));
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
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.
I have a date range in which I want to process between each day. So for example between
2013-03-01 00:00:00 and 2013-04-01 00:00:00 there are 31 days
so my for loop is something like this
$date_next = $date_from;
for($i=0;$i<31-1;$i++)
{
$date_next_str = new DateTime($date_next);
$date_next_1_str = new DateTIme($date_next);
$date_next_1_str->modify("+1 day");
$date_next_1_str->modify("-1 second");
$date_next_1_str->modify("+1 day");
$date_next = $date_next_1_str->date;
}
so in my first loop it will be from 2013-03-01 00:00:00 to 2013-03-01 23:59:59
However when I assign $date_next_1_str->date to $date_next at the end of the for loop, the $date_next still shows 2013-03-01 23:59:59, which was supposed to be 2013-03-02 00:00:00.
Anyone can help me with this? Thanks in advance!
You can do this quite easily using PHP's DateTime, DateInterval and DatePeriod objects:-
$startDate = \DateTime::createFromFormat('Y-m-d H:i:s', '2013-03-01 00:00:00');
$endDate = \DateTime::createFromFormat('Y-m-d H:i:s', '2013-04-01 00:00:00');
$interval = new \DateInterval('P1D');
$endDate->add($interval); //As otherwise last day will be missed off.
$period = new \DatePeriod($startDate, $interval, $endDate);
foreach($period as $date){
//each $date is an instance of DateTime
var_dump($date); // Or whatever you want to do with the DateTime object
}