PHP - Parsing String As Past Date - php

Currently, I'm trying to parse out dates when messages were received into timestamps. I have the month and day but the year is not specified. The event always occurs at the most recent (human) reading of the time. It works great in most cases to do this:
$time = strtotime("Jan 2 8:38pm");
That returns a date for this year, which is correct. Unfortunately, I get problems when I try to do for example:
$time = strtotime("Dec 31 8:38pm");
That returns a date which hasn't happened yet, and wont happen for the whole rest of the year. Obviously, my message was not sent in the future. I need it to return December 31st of last year.
For weekdays, I had a solution by prepending 'last' before the weekday like so:
$time = strtotime("Last Saturday 8:38pm");
That always returned the time of the last Saturday. However, trying to do the same thing here doesn't work:
$time = strtotime("Last Dec 31 8:38pm");
This returns false. I know to decrement a date by 1 year, I can do this:
$time = strtotime("Dec 31 8:38pm -1 year");
And that works great for Dec 31. However, Jan 2 will now fail:
$time = strtotime("Jan 2 8:38pm -1 year");
One solution I thought of was to subtract off a year (86400 * 365) from the resulting value if it is past today's date. However, this result will fail if we passed over February of a leap year. In that case, we would end up with a time that was ahead by a day.
The best solution I came up with so far is this:
$time = strtotime($raw_time);
if ($time > time()) {
$time = strtotime($raw_time." -1 year");
}
It seems kind of wasteful to make two calls to strtotime which I know is probably not a very efficient function. Is this the most elegant solution?
Is anyone aware of an option in strtotime which forces the dates to be in the past instead of in the future?
Is there another way to parse these dates that I should consider?
Efficiency is important for this because I am going to be parsing a lot of dates with it, but I would also like simple and readable code so I can understand it later.

Your approach is fine, as there is no date format to get what you want. Another approach could be using the DateTime class:
$datetime = new DateTime($raw_time);
if ($datetime > new DateTime()) {
$datetime->modify('-1 year');
}
You could test which one of the two approaches is faster. My guess is that this is a micro-optimization that won't make a lot of difference.

Related

Find next occurance of a given time after given timestamp

Given an arbitrary timestamp (e.g. 2019-02-26 10:30:00) I would like to find the next occurrence of an arbitrary time.
For example, the next occurrence of 12:00:00 will be 2019-02-26 12:00:00 but the next occurrence of 09:00:00 will be the next day at 2019-02-27 09:00:00. The results could be Carbon or Datetime objects. The test time will just be a string as shown.
Is there a way to calculate this in native PHP or PHP Carbon without conditionally boxing in time periods. An obvious way would be to see if the time being tested is past the check time for today, and if it is, taking the result as the check time plus 24 hours (the next day). That feels to me like too much chopping and joining of dates and times, so is there a way to calculate it by considering time to be a simple linear line?
All times will be in a single timezone, with DST. Note: the arbitrary datetimes and check times will stay clear of DST changeovers i.e. 01:00 to 02:00 so hopefully they will not be an issue to take into account.
Short answer is no for PHP (partial answer, I'm no specialist of Carbon but from quick look it's also no, but you can create a macro from following code).
However, with a ternary condition the one-liner is simple enough IMHO (replace the second DateTime($str) with DateTime() if you want to compare with current date and time, and change the >= by > if you want next day when time compared is exactly the same):
$str = '2019-02-26 10:30:00';
$date1 = ( ($a = (new DateTime($str))->setTime(12,00)) >= (new DateTime($str)) ) ? $a : $a->modify('+1 day');
$date2 = ( ($a = (new DateTime($str))->setTime(9,00)) >= (new DateTime($str)) ) ? $a : $a->modify('+1 day');
echo $date1->format('Y-m-d H:i:s'); //2019-02-26 12:00:00
echo $date2->format('Y-m-d H:i:s'); //2019-02-27 09:00:00
quick note: what you gave us is not a timestamp, but a formatted date.
Here is what I am using now through Carbon, which appears to give me the correct results:
$dateTime = Carbon::parse('2019-03-30 17:34:50', 'Europe/London');
$testTime = '16:00:00';
list ($hour, $minute, $second) = explode(':', $testTime);
$nextTimeOccurrence = $dateTime
->copy() // Carbon 1 only
->hour($hour)->minute($minute)->second($second);
if ($dateTime->gt($nextTimeOccurrence)) {
$nextTimeOccurrence = $nextTimeOccurrence->addDay();
}
// $nextTimeOccurrence is the next occurrence of $testTime after $dateTime
The splitting of the time seems clumsy, but might be the best way? The approach is:
Create a timestamp with the test time on the same day as the timestamp I'm checking. This will be the timestamp I am looking for.
If the timestamp I'm checking is after the timestamp created in the previous step, then add a day to it.
I've tested this around DST, and happily Carbon/Datetime keeps the same time when adding a day over a DST period, where a day there would be 25 hours or 23 hours, depending on which way it goes.
I still think there is a more "linear time" way to do this, but this seems simple and robust. Thanks go to #michael-stokoe here at the office for my lead on this.

PHP method strtotime is not resulting well

function DateFormat($dt)
{
return $newDate = date("d/m/Y", strtotime($dt));
}
$cr='2014-02-31';
echo DateFormat($cr);
Input: $cr='2014-02-31';
Output: 03/03/2014
I am passing 2014-02-31 and getting output 03/03/2014.
Please help me out.
PHP's date functions work with dates not strings. And that's an important distinction. Strings are just a bunch of characters in a specified order. Dates have months, days, years, hours, minutes, seconds, timezones, etc. When PHP works with dates it takes all of them into consideration.
So when you pass Feb 31 to a PHP date function it is going to try to make sense of it as a date and not a string. This means it isn't just going to take that date cut it up into bits and then rearrange them as you are expecting. It is going to turn that date into a date representation it can work with and then manipulate it.
As we all know, February does not have 31 days. As a result of the invalid date, PHP is trying to be helpful and taking three days after last day in February of that year (since Feb only has 28 days this year) and giving you that date.
The issue is February most years only has 28 days. 2/31 would be logically translated to 3/3. On a leap year you'd get 3/4...
The strtotime() method as it needs to be very flexible to be able to handle stuff without borking like:
strtotime('2014 February + 31 day - 1 year');
And no I don't think it should error out. When you have a well formed date string, PHP has a deceptively named method called checkdate() you could use:
$crappy_date='2014-02-31';
$date_parts = explode('-', $crappy_date);
$valid = checkdate($date_parts[1], $date_parts[2], $date_parts[0]);

php strtotime displaying month as day

I am constructing a diary that has weekly views which I thought I had cracked because the dates seemed to appears as correct. It wasn't until my MySQL queries kept returning what seemed like random results that I realized the the month is actually being seen as the day instead.
$week_number = date("W");
$year = date("Y");
for ($day=0; $day<=6; $day++) {
$daily_date = date('d/m/Y', strtotime($year."W".$week_number.$day))."\n";
$StartDate = date('d', strtotime($daily_date));
}
echo $starteDate;
$startDate returns the number of the month rather than the day and sure enough date('m', strtotime($daily_date)) returns the day rather than the month.
I can't understand where I have made this silly mistake so any help would be appreciated.
This is because of the Americanisation of dates - strtotime will read the date as m/d/Y rather than d/m/Y.
The ISO for dates is Y-m-d and for ease I would use this format when doing any kind of date manipulation.
That code is horrible. You're converting dates to strings multiple times. There is absolutely NO reason to take your $year . w. $week_number.etc... value, convert it to a string, then convert that string back to a date just to extract the day value.
As well, d/m/Y is a horrible format to use for date transport, because... riddle me this, what is 01/02/03. Is that 3rd Feb 2001? 1 Mar 2002? If you can't figure it out, how can you expect strtotime to be better at it? it's fairly smart, but it's not omnicient, and it's DEFINITELY not infallible. A 4digit year does make it a bit easier, but you can still end up with d/m v.s. m/d confusion.
Why not simply
$StartDate = date('d', strtotime($year."W".$week_number.$day));
or better yet, use the DateTime class and select an appropriate DateInterval

How to get last "particular (Ex. March)" month in PHP?

I want to display the last March month. I am using the following code,
date("Y-m-d",strtotime("last March"));
Any suggestions or references would be very helpful.
$year = date('Y');
if (date('n') < 3) {
$year--;
}
$beginningOfLastMarch = mktime(0, 0, 0, 3, 1, $year);
See http://php.net/date
date('M')
or
date('n')
or
date('m')
or, if you have a date
$mydate = "2010-05-12 13:57:01";
you can get like this
$month = date("m",strtotime($mydate));
m Numeric representation of a month, with leading zeros 01 through 12
n Numeric representation of a month, without leading zeros 1 through 12
I found a better way to do this but left my old solution at the bottom, as it may still be relevant for some people in certain use cases.
I found a better solution to my problem which involves prepending a date to strtotime, and then using the relative selectors. Say I wanted to select the 4th of March last year. I just do:
strtotime('{THIS_YEAR}-03-04 last year');
Obviously, before processing this string, I would replace {THIS_YEAR} with date('Y'). This works, simply because I hard code the values I want. 03-04 as 4th of March. I can replace these numbers with any date I like.
Old Solution
I figured out a solution that worked for me and it doesn't really involve writing any complex algorithms or anything. It does involve slightly extending the syntax of strtotime though.
Even though PHP's strtotime isn't perfect (for all use cases), I found that if you join strtotime's together, then you can make it extremely powerful.
Say if I wanted to select the 4th of last month, I can't really do that... strtotime doesn't really accept ordinals. I.E 4th.
However, I can do first day of last month, which is pretty close. All I need is to change the day.
strtotime allows a time() to be passed as a second argument. This means you can chain strtotime's like so:
$time = time();
$time = strtotime('first day of last month', $time);
$time = strtotime('+3 days', $time);
echo date('Y-m-d H:i', $time);
// I know we don't really need the initial `$time = time()`. It is there for clarity
which will give us the right time: 4th of last month.
This is where a little syntactic sugar comes in...
We can write a function that accepts partial strtotime strings separated by a delimeter:
function my_strtotime($str, $time = null, $delim = '|'){
$time = ($time==null ? time() : $time);
foreach(explode($delim, $str) as $cmd){
$time = strtotime($cmd, $time);
}
return $time;
}
Which can be used like so:
$str = 'first day of last month | +3 days';
echo date('Y-m-d H:i', my_strtotime($str));
And there we have it. 4th of last month.
There is no need to strip whitespace from the explode call, because strtotime already handles extra whitespace.
This can handle complex intervals like last year | first day of March | +9 days | 14:00 which will always return 10th of March at 2pm last year.
The best thing about this is that strings like last year | first day of March | +9 days | 14:00 can be generated with the required values, E.G.
last year | first day of {MONTH} | +{DAYS-1} days | {TIME}
This might need extra work to improve it, but I just wanted to quickly get this out here, so it may help others reaching this question

Understanding date processing with strtotime

I'm trying to get my head round someone else's code which they've written for handling the dates of when news stories are published. The problem has come up because they are using this line -
$date = strtotime("midnight", strtotime($dateString));
to process a date selected using a jquery calendar widget. This works fine for future dates, but when you try to use a date which is in the previous calendar year, it uses the current year instead. I think this is due to "midnight" finding the closest instance of the selected day and month.
I could remove the "midnight", but I'm not sure what the repercussions of this would be - is there a reason that the midnight could be there?
EDIT: this is the full block of code which handles the date. The date contains the time, which allows the user to publish an item at a specific time.
$array['display_date'] = '24 October, 2011 17:30';
$string = $array['display_date'];
$dateString = substr($string, 0, -5);
$timeArray = explode(':', substr($string, -5));
$hours_in_secs = 60 * 60 * $timeArray[0];
$mins_in_secs = $timeArray[1];
$date = strtotime("midnight", strtotime($dateString));
$timestamp = $date + $hours_in_secs + $mins_in_secs;
//assign timestamp to validation array
$array['display_date'] = $timestamp;
echo $array['display_date']; // Output = 1351094430 (Oct 24 2012 17:00:30)
This really depends on what $dateString contains. Assuming your jQuery widget delivered the time portion as well, your colleague likely wanted to remove the time portion. Compare the following:
echo date(DATE_ATOM, strtotime('2010-10-01 17:32:00'));
// 2010-10-01T17:32:00+02:00
echo date(DATE_ATOM, strtotime("midnight", strtotime('2010-10-01 17:32:00')));
// 2010-10-01T00:00:00+02:00
If your widget doesnt return the time portion, I dont see any reason for setting the date to midnight, because it will be midnight automatically:
echo date(DATE_ATOM, strtotime('2010-10-01'));
// 2010-10-01T00:00:00+02:00
Note that all these are dates in the past and they will result in the given year in the past, not the current year like you say. If they do in your code, the cause must be somewhere else.
Will there be repercussions when you change the code? We cannot know. This is just one line of code and we have no idea of any context. Your unit-tests should tell you when something breaks when you change code.
EDIT after update
The codeblock you show makes no sense whatsoever. Ask the guy who wrote it what it is supposed to do. Not only will it falsely return the current year for past years, but it will also give incorrect results for the minutes, e.g.
24 March, 2010 17:30 will be 2012-03-24T17:00:30+01:00
I assume this was an attempt at turning 24 March, 2010 17:30 into a valid timestamp, which is in a format strtotime does not recognize. But the approach is broken. When you are on PHP5.3 use
$dt = DateTime::createFromFormat('d F, Y H:i', '24 March, 2010 17:30');
echo $dt->format(DATE_ATOM); // 2010-03-24T17:30:00+01:00
If you are not on 5.3 yet, go through https://stackoverflow.com/search?q=createFromFormat+php for alternate solutions. There is a couple in there.

Categories