I'm getting incorrect results when adding an interval in a DateTime input string.
Some strings work, some don't. Obviously adding zero shouldn't put the result 5 days in the future and truncate the time. Yikes.
$dtTestStr='2018-09-05 10:25:00-04 + 0 year 0 mon 0 day 00:00:00';
$myFubar=new DateTime($dtTestStr);
echo $dtTestStr. ' is now '.$myFubar->format('Y-m-d H:i:s');
// '2018-09-05 10:25:00-04 + 0 year 0 mon 0 day 00:00:00 is now 2018-09-10 00:00:00'
First of all, you need to use 'month' or 'months' instead of 'mon' and once you eliminate that problem, you have another error. It doesn't know what to do with those zeros. I also removed an extra ) at the end of your echo statement.
You want it to look like this:
$dtTestStr='2018-09-05 10:25:00-04 + 0 year 0 months 0 day 0 hour 0 minute 0 seconds';
$myFubar=new DateTime($dtTestStr);
echo $dtTestStr . ' is now ' . $myFubar->format('Y-m-d H:i:s');
Here's a sandbox link: Sandbox
The DateTime class thinks that you are asking for the Monday (mon) that follows September 5th, 2018. The following monday is the 10th.
Spelling out month or months will fix your typo.
To be clear, you don't need to assign all those zeroed intervals.
Here's some informative reading regarding several different ways to modify your time data.
Adding minutes to date time in PHP
Related
I think I am correct in saying that strtotime(2023) will return the number of seconds from 1 Jan 1970 to 1 Jan 2023.
I thought I could then add a year to that value like this: strtotime('+1 year', strtotime(2023)). I assume that the the number of seconds added would take into account whether it is a leap year or not and even whether any leap seconds need to be accounted for. However I am not sure if that assumption is correct.
If my assumptions were correct then the value should be the same as strtotime(2024). When I do it I find the ouputs are not the same.
The code I have run and the output is shown below:
<?php
$tsFrom = strtotime(2023);
$tsTo = strtotime('+1 year', $tsFrom);
echo $tsTo;
echo ' | ';
echo strtotime(2024);
// output:
// 1705436580 | 1673900640
I ran the code on the sandbox at https://onlinephp.io/
I tried putting the argument to strtotime() in quotes as I know normally it would take a string but that makes no difference to the output.
I would like to know where I am going wrong
strtotime(date('Y-m-d', strtotime(' + 1 years')))
or
strtotime(date('2022-01-16', strtotime(' + 1 years')))
I have a task module, in it I have two fields
t_due_on(task complete date)
t_completed_on (task completed date)
Example data:
id task_name t_due_on t_completed_on
1 PF Module 2017-03-14 10:15 PM 2017-03-13 23:29 PM
The given task's due date is tomorrow but it was actually finished today, so I need to display the results like:
Task finished: 1 day before
You can use the DateTime::diff() or DateTimeImmutable::diff() function. All you have to do is to compare the two dates. I have given a very basic example here:
$dt1 = new DateTimeImmutable('2017-03-14 00:00:00');
$dt2 = new DateTimeImmutable('2017-03-16 12:00:00');
$di = $dt2->diff($dt1);
$outputStr = "Task finished: {$di->days} days ";
$suffix = $di->invert == 1 ? 'before' : 'after';
$outputStr .= $suffix . " deadline";
echo $outputStr;
Now it is important to note that:
the diff() function will produce a DateInterval class instance. This does not provide methods but rather explains the time interval in terms of seconds, minutes, hours, days, weeks etc.
notice that $di->invert is high (== 1) when the interval is -2 days for eg. $di->invert will be 0 for positive differences
the order of comparison is important! So calling $dt2->diff($dt1) is not the same as $dt1->diff($dt2) because of how the dates are spaced in time
you have to take care of the output. I.e. IF the difference between the two dates is less than a day $di->days will be 0! But $di->h will be set. Thus your functionality has to intelligently handle this also
I have two times saved in database as
DayTime1 = "Wed 09:00"
DayTime2 = "Wed 13:00"
I want to get the difference between these two dates in minutes.
TIMESTAMPDIFF(MINUTES,DayTime1,DayTime2) return null
I'm not able to get through it. Is there any way to get difference?
Please note, that SELECT STR_TO_DATE("Wed 09:00", "%a %H:%i") returns 0000-00-00 09:00:00 (at least on my local MySql 5.5.16). So if comparing different days, you won't get the correct result.
If given a year and week, the day name will be interpreted to a real date, so comparisons may also span days. For example (though not really elegant, I admit):
SELECT TIMESTAMPDIFF(MINUTE,
STR_TO_DATE(CONCAT(YEAR(CURDATE()), WEEKOFYEAR(CURDATE()), ' Tue 09:00'), '%x%v %a %H:%i'),
STR_TO_DATE(CONCAT(YEAR(CURDATE()), WEEKOFYEAR(CURDATE()), ' Wed 13:00'), '%x%v %a %H:%i')
)
Since you also included the php-tag, I'm assuming a PHP solution is valid as well:
$daytime1 = "Wed 09:00";
$daytime2 = "Wed 13:00";
$diff = abs(strtotime($daytime1) - strtotime($daytime2)); //absolute diff in seconds
$diff = $diff / 60; //Difference in minutes
EDIT:
And here is a pure MySQL solution:
SELECT ABS(
TIME_TO_SEC(
TIMEDIFF(
STR_TO_DATE("Wed 09:00", "%a %H:%i"),
STR_TO_DATE("Wed 13:00", "%a %H:%i")
)
)
) / 60
The second parameter, "%a %H:%i"is the date format. If it's always in the format of "First three letters of weekday, space, hour with leading zero, :, minutes" then you can use this format.
I have a problem with my script and I dont understand where is the problem. So I have this code :
$i_now = strtotime(date('Y-m-d'));
$i_date_last_bonus = strtotime($o_member->date_last_bonus);
$i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400);
print_r("Date Now :".date('Y-m-d'));
print_r("Last Win :".$o_member->date_last_bonus);
I get the $i_datediff = 1 and I dont understand why because in the print_r I have Date Now :2015-12-04 and Last Win:2015-12-03
Can you help me please where I make the error ? Thx in advance and sorry for my english
In one day there are 24 Hrs, in each hour there are 60Min, in each min there are 60sec. Hence, there are 24*60*60 = 86400sec in one day.
Now, The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Means it returns seconds.
so, i_now = 1449187200 andi_date_last_bonus = 1449100800
Difference is 86400sec.
Now $i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400); this is converting seconds in days.
And Difference is 86400 / 86400 = 1
means 1 day.
This result is correct as you get the number of seconds between two dates first and then you divide it by the number of seconds in 24 hours (86400) which gives you 1 (day) as a result.
I am trying to set publish date based on user choice and give it interval in a loop. But after it is substituted with the intervals, the year changed to the current year.
Here is the sample of my code:
$datestart = "2012-03-06";
$datenow = date("$datestart H:i:s", current_time( 'timestamp' ));
$newpostdate1 = $datenow + strtotime("0 years 0 months 1 days 0 hours 0 minutes 0 seconds");
$newpostdate = date("Y-m-d H:i:s", $newpostdate1);
echo $datenow . " " . $newpostdate;
$datenow Will return 2012-03-06 16:19:33 while $newpostdate return the current date plus 1 day i.e: 2014-03-15 17:02:23.
Why $newpostdate returning the current date plus next 1 day instead of 2012-04-06 16:19:33 ?
..because what you're doing doesn't do what you think it does.
First, you set $datenow to a string (not a date object), with value "2012-03-06 " + the current time (assuming that's what current_time returns).
Then you call strtotime with the value "1 days" (well, your string has a bunch of other zero-valued fields, but they don't change the result), which returns the current time + 24 hours as a number (the number of seconds since 1970).
Then you take that value and add it with + to the above string. This causes the string to be interpreted as a number, so it turns into 2012 (and the rest of the string is ignored). So the result is a timestamp representing the current time + one day + 2,012 seconds - or one day, 33 minutes and 32 seconds from the time the code is run. Which you then format as a string.
You could use John Conde's solution to get a more meaningful result. (I assume your real problem is different, else why not just start out by setting the string to '2012-03-07' in the first place?)
The first parameter of date() is the format you want the timestamp passed as the second parameter to be displayed as. So basically you are using date() incorrectly.
I think this is what you are looking for:
$date = new DateTime($datestart);
$date->modify('+1 day');
echo $date->format(Y-m-d H:i:s);