PHP DateInterval - Where is string $format parameter defined? - php

Been trying to figure out just how to add hours, days, weeks and years to a date. Found a few examples that work, but I have NO idea why.
$dt->add(new DateInterval('P1Y')); 'P1M', 'P1D' all add one year, month and day. 'P1H' or 'P1S' throw exceptions.
Been reading all about DateTime class and reading the https://www.php.net/manual/en/dateinterval.format.php page, NO WHERE can I find anything that explains what the 'P' part of that format string is.
Where is some decent documentation on this??? It should not take hours to figure out how to add a few days to a date!!

The P stands for period. If you want to define an interval based on hours or minutes check this example:
$interval = new DateInterval('PT1H');
Here, $interval represents a time interval of 1 hour.
For example, to add an hour to an existing date:
$date = new DateTime();
$date->add($interval);

Related

Wrong time with DateTime

I am creating notifications system and here is a task: get the time, when a notification was sent. I mean the following: 1 minute ago, 13 hours ago and so on. I have already made up a script but it shows wrong time. For example instead of showing '5 minutes ago' it shows '9 hours ago'. Here is the alrogithm:
Get old timestamp from database. Old timestamp is the time, when a
notification was sent.
Get current users timestamp.
Get difference between them.
Echo result.
Here is the PHP code:
$fromdb = '1503737539'; //For this example think, that this variable is from database.
//This timestamp was created 5 minutes earlier, so in result it should show '5 minutes ago'.
$curr = new DateTime();
$got2 = new DateTime(date('Y-m-d H:i:s',$fromdb));
$interval = $curr->diff($got2);
echo $interval->format('%d')." days ".$interval->format('%h')." Hours ".$interval->format('%i')." Minutes ".$interval->format('%s')." Seconds";
The output is:
0 days 9 hours ....
instead of
0 days 0 hours 5 minutes ....
How can I fix that? I guess that this is a problem with timezones. But how can I guess guests timezone though?
Update:
Change code:
$got2 = new DateTime(date('Y-m-d H:i:s',$fromdb));
And interesting fact: The more is actual difference, the less time it shows in output. For example: Old timestamp was created at 8:00 am, and current is 15:00 pm, it shows 0 days 1 hour in output.
I think
date('Y-m-d',$fromdb)
broke your code.
Try
$got2->setTimestamp($fromdb)
there is mistake in your code. you format date without hours, minutes and seconds
$got2 = new DateTime(date('Y-m-d',$fromdb));
try
$got2 = new DateTime(date('Y-m-d H:i:s',$fromdb));
Try
$date = new DateTime(); echo $date->format('U = Y-m-d H:i:s');
$date->setTimestamp(1171502725);
echo $date->format('U = Y-m-d H:i:s');

PHP Show next 7 Days range

I saw this response to a thread and it works but doesn't create a range between the current day and seven days time. Which is what I need. Can anyone give me a hand please?
Update:
An easier way to word it. I want to Select data between two dates. For example current day and seven days time.
My current code:
if($current_day) {
$data['current_day']=date('Y-m-d', strtotime('+7 days'));
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%a days, %h hours, %i minutes, %s seconds");
You can use this code sample if you have php 5.3 or above,
Else try to calculate the difference of two dates in seconds using time() and strtotime(). Then translate those seconds into days/hours/minutes/seconds.

php DateTime diff - include both dates in range?

I have been using DateTime Diff (in php) to get various settings for pairs of dates - two formatted dates to display, a difference from the date to now (eg "start date was 3 months 2 days ago"), and a length between the two dates ("length is 2 months 3 days").
The problem is that DateTime Diff ignores one of the days so if the start is yesterday and the end is tomorrow, it gives 2 days whereas I want 3 because both dates should be included in the length. If it was just days, I could simply add 1 to the result, but I wanted to use the years/months/days results from the Diff and these are determined at construct.
The only way I have found to get the desired results is to create a DateTime for start and end (to get the formatted dates and the differences). Then take the end DateTime, add 1 day to it, then work out the length.
It's a bit clunky but there seems to be no way to tell DateTime Diff to include both start and end dates in the result.
DateTime encapsulates a specific moment in time. "yesterday" is not a moment but a time range. The same for "tomorrow".
DateTime::diff() doesn't ignore anything; it just provides you the exact difference (in day, hours, minutes a.s.o.) between two moments in time.
If you want to get the diff between "tomorrow" and "yesterday" as "3 days" you can subtract the first second of "yesterday" from (one second after the last second of "tomorrow").
Like this:
// Always set the timezone of your DateTime objects to avoid troubles
$tz = new DateTimeZone('Europe/Bucharest');
// Some random time yesterday
$date1 = new DateTime('2016-07-08 21:30:15', $tz);
// Other random time tomorrow
$date2 = new DateTime('2016-07-10 12:34:56', $tz);
// Don't mess with $date1 and $date2;
// clone them and do whatever you want with the clones
$yesterday = clone $date1;
$yesterday->setTime(0, 0, 0); // first second of yesterday (the midnight)
$tomorrow = clone $date2;
$tomorrow->setTime(23, 59, 59) // last second of tomorrow
->add(new DateInterval('PT1S')); // one second
// Get the difference; it is the number of days between and including $date1 and $date2
$diff = $tomorrow->diff($yesterday);
printf("There are %d days between %s and %s (including the start and end date).\n",
$diff->days, $date1->format('Y-m-d'), $date2->format('Y-m-d')
);

PHP DateTime diff

This may be a complete noob question but here goes:
I have the following code that compares two dates for absence management. Where I expect the answer to return as 2 (the difference between start and end date) I get 1.
$start_time = new DateTime("2015-01-01 00:00:00");
$end_time = new DateTime("2015-01-02 00:00:00");
$diff = $end_time->diff($start_time);
$d = $diff->days; // 1
I have also tried using just the dates (but I need the times as some absence type are done by hours not days)
Difference is 1 because there is only one day difference between both days.
To convert the datetime into hours or minutes you should look to these links:
Convert datetime into year, month, days, hours, minutes, seconds
Difference between 2 time() values

Using DateInterval to add weeks to a DateTime object

Currently my code is adding a week, but I'm using Days instead of Weeks. I've read the documentation and don't quite understand how it works.
PHP: DateInterval
# Adds 7 days to the project launch date.
$project_launch_date->add(new DateInterval('P7D'));
Instead of adding manual 7 days, how can I specify, 'add a week', or 'add n weeks'?
If DateInterval is unclear for you, you can use more clear modify of DateTime class.
$date = new DateTime();
$date->modify('+1 day');
$date->modify('+5 week');
I prefer to use modify, because it makes code more readable without comments
In case you prefer to use DateInterval, here is good reference: http://www.php.net/manual/en/dateinterval.construct.php
So 5 weeks will be P5W, 3 month will be P3M, 5 weeks AND 3 month P3M5W and so on.

Categories