Hi Everyone,

I need to check if a date is valid but not relative. Here's what I came up with:

if ( strtotime ( $date ) && strtotime ( $date ) == strtotime ( $date, 1 ) ) {
    $its_all_good = true;
}

If that's helpful, then I'm glad I can help. If it's nuts, please let me know what the better way is.

Thanks,
Ben

Member Avatar for diafol

What does relative mean? Do you mean like 'last week' or something like that?

I'm not sure that strtotime ( $date ) == strtotime ( $date, 1 ) is right. My understanding is that the second parameter stands for the base timestamp for the relative dates. So this would be at 1 second after the start of Unix Time, back in 1970.

So unless you had an exact match to that particular calculated timestamp, you'd get false - so you'd probably never get true.

If you're trying to see if a 'date' is not equal to the current date, there is a easier option:

$dt = new DateTime($date);
if($dt->format('Y-m-d') !== date('Y-m-d'))
{
    echo 'boo';
}else{
    ech 'yay';
}

There are many ways you could do the above, that's just off the top of my head.

Hi Diafol,

Thanks for the reply. To the first part: yes, that's what I mean but I also mean dates like "January 1st", which will change value every year.

My logic is that strtotime ( "January 1st, 2020", $x ) is going to be January 1st, 2020 whether $x is today or $x is 45 years ago, but strtotime ( "January 1st", $x ) is going to change depending on the value of $x.

My value 1 as the second parameter is somewhat arbitrary, as long as it's a date that will not to be used, which is true for my purpose.

Member Avatar for diafol

What input are you going to accept? THat is, what formats?

In the application that I'm working on, I have a series of formats that I'll be testing against, but my question was more general. I was thinking about a way to take any format that strtime will accept, and test if it's relative or absolute. Another way to put it, I think, is: did the user enter a year? If not, then it's an annual event.

For the app I'm working on, I might just end up using regex to figure out if the user entered a year. I haven't quite decided yet.

Member Avatar for diafol

I think this is more complex that it seems at first glance. If you're just checking whether a year have been entered, then a \d+{4} should suffice. But a strtotime without a year assumes the current year.
I'm assuming you have an input box into which a user can enter the date as a string? If not, how is the user supplying the info?

PS. are users allowed to enter things like 'Every Tuesday'?

Instead of testing for each case, perhaps it would be easier for you to limit the input format or different input methods, e.g. repeating events vs one-off?

I'm trying to write an app that will read text the way some apps read ical text. But I want my text to be in plain english, so people can write a list like,

Every Tuesday
Lunch with Bill

Every 2nd Wednesday of the Month
Department Meeting

July 4th  
Independence Day

July 4th, 2015
BBQ with the Smith's

. So there's no input form.

I wanted to first run the date through strtotime, and then use my own procedures if strtotime fails. "Every Tuesday" will fail, so my code will take over. But "July 4th" and "July 4th, 2015" will both pass strtotime, and I wanted a way to tell the difference between the two.

What I came up with will work, but I wasn't sure if it was the best way.

I'm getting worried that I'm leading you down a rabbit hole. I really appreciate the responses but I'm afraid I might not need to do what I was originally trying to do. In earlier versions of my script, it was important to separate the absolute dates, but now I think it a mistake to do so. The dates will work as expected. Next year, the BBQ won't match the current date even on July 4th, but the holiday still will.

Member Avatar for diafol

iCal uses iCalendar AFAIK, so you're using a structured markup language such as 'VEVENT'. I'm not sure if calendar apps (in general) read plain text, do they? I don't use iCal so I can't comment.

strtotime is a bit hit or miss for me, so using it as a natural language parser may be a bit dangerous. If strtotime doesn't recognize it, then you follow up with what? Probably a long list of things.

Are we correct to assume that the 'date' could be in any format, such as...

"Every Saturday starting next month for 3 months, but not my birthday on the 10th of October" (heh heh - only half joking)

I was just using iCal as the closest thing I can think of. iCal is not plain text in human-readable sort of way, but it's plain-text in a file-format sort of way. Mine will be both.

What I have now, and probably going to stick with, is a bunch of regex tests to figure out what the user's intent is. The problem with strtotime is it accepts too many phrases, including some that I can't accept given what I'm trying to do.

Half laughing along. I've gotten pretty close. There is no "next", because every day that it reads the file "next" might mean something different, but I do allow "every Third Saturday starting July 25th, 2015 except the 10th of October, 2015". Unfortunately, the user has to enter the year unless s/he is entering a recurring event, and interval events must have a starting date with a year.

It's a WordPress plugin, btw. You can have one page of "imporant dates" or something like that, and another that displays those dates as a calendar. It's the first plugin that I plan to submit.

Member Avatar for diafol

Ah right. Now the fog parts. Good luck with it. Extracting data getting the exact intent of the user is no mean feat. Not something that I'd be able to help with in a forum post. But if you're looking for good regex, there's a cute little site I dip into often:

http://regexlib.com/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.