Argh PHP Help my Brain time...


Author Reply
peej
Flag
Posts:14637
Comments:4691
Thread Kills:462(3%)
AATG Pts:400
Star Rating
Gold Medal
Right then. Trying to find out whether a specified date string (in the format YYYYMMDD) falls between two other dates in the same format. You'd think this would be pretty simple in PHP but pretty much everything I've tried doesn't work predictably...

So what am I doing wrong? So far I've done stuff along the lines of $today = strtotime(date(Ymd))

then

if $today > $enddate then do something
else if $today < $startdate then do something else...

but it doesn't seem to recognise that my date falls between two days.

Any help muchly appreciated.
#1 at 16:20:40 - 01/12/2008
HairyArse
Flag
Posts:6388
Comments:1774
Thread Kills:127(2%)
AATG Pts:350
Star Rating
Gold Medal
Hmm strtotime should work perfectly. I've done something similar but I used:

$today = date ("Y-m-d H:i:s");
$today = strtotime($today);

And then did my comparisons.
#2 at 16:29:46 - 01/12/2008
peej
Flag
Posts:14637
Comments:4691
Thread Kills:462(3%)
AATG Pts:400
Star Rating
Gold Medal
I'm trying to avoid over-complicating the date string so a YYYYMMDD one was what I was after. Basically the date will be put in by an academic, so keeping it as simple as poss is pretty much the only option.

Strtotime on that string seems OK, it's just when I run the comparisons the fucking thing ignores what I'm asking it to do.
#3 at 16:36:11 - 01/12/2008
HairyArse
Flag
Posts:6388
Comments:1774
Thread Kills:127(2%)
AATG Pts:350
Star Rating
Gold Medal
Hmm the only thing I can think is that maybe strtotime requires some kind of a seperator between the datal elements, but then again it's an extremely powerful function and lets you use strings such as "4 days" "tomorrow" or "yesterday" so you'd think it'd be OK with what you're trying to do.
#4 at 16:40:41 - 01/12/2008
peej
Flag
Posts:14637
Comments:4691
Thread Kills:462(3%)
AATG Pts:400
Star Rating
Gold Medal
Tried it with a separated date/time format and it still gives the same results.

Bah...could just be the syntax of my switch case statement but it LOOKS right...
#5 at 16:46:48 - 01/12/2008
eviltobz
Flag
Posts:304
Comments:68
Thread Kills:15(5%)
AATG Pts:80
Star Rating
Bronze Medal

if $today > $enddate then do something
else if $today < $startdate then do something else...

to me, that reads as "if today is after enddate, do something, if today is before startdate, do something else", so if today is between startdate and enddate then no condition will be met.

to check if a date is between 2 dates you want something like:
if(mydate >= startdate and mydate
#6 at 16:47:19 - 01/12/2008
HairyArse
Flag
Posts:6388
Comments:1774
Thread Kills:127(2%)
AATG Pts:350
Star Rating
Gold Medal
Wanna paste or send me your code and I'll have a look? It sounds like one of those issues where it'll be something so minor that you can't see it.

:)
#7 at 16:47:19 - 01/12/2008
peej
Flag
Posts:14637
Comments:4691
Thread Kills:462(3%)
AATG Pts:400
Star Rating
Gold Medal
Solved it. Went back to basics with if elseif else rather than case-switch. So yeah, it was one of my "blonde" moments I think - where I thought the solution was more complicated than it actually was.

Ah well, simplest solutions are often the best. Ta though.
#8 at 16:53:20 - 01/12/2008
peej
Flag
Posts:14637
Comments:4691
Thread Kills:462(3%)
AATG Pts:400
Star Rating
Gold Medal
Gnnn!

I thought I knew how to do this. Say I want to trim everything in a string after a separator character - for instance - I have a string that says

"I suck from the cock of goats, I am a bad person"

and I want to chop the end off that string after the comma.

I thought it was "explode" but I can't seem to get that to work. Bear in mind the string's part of an array which could be why explode doesn't work...

Any ideas, geniuses?
#9 at 11:27:20 - 29/01/2009
Salaman
Flag
Posts:1241
Comments:40
Thread Kills:88(7%)
AATG Pts:130
Star Rating
Bronze Medal
PHP related

/isn't as helpful as it may seem
#10 at 15:13:56 - 30/01/2009
peej
Flag
Posts:14637
Comments:4691
Thread Kills:462(3%)
AATG Pts:400
Star Rating
Gold Medal
He's right too. It's a lovely "lazy programmers" language. I can't code in anything properly OOP to save my life, but knocking together PHP is (usually) pretty easy and gives you more or less instant results.
#11 at 15:19:24 - 30/01/2009
mal
Flag
Posts:532
Comments:165
Thread Kills:40(8%)
AATG Pts:85
Star Rating
I'm not a PHP expert, but in most languages you'd use the index and substr functions, like:

$input="one,two"
$chopped=substr($input,0, index($input,","));

In other words, get the text from the beginning of the string up to (but not including) the character where the first comma is.

Or if you've got regular expressions, you could do:

$input="one,two"
$chopped=$input
$chopped=~s/,.*$//

Which means 'replace a comma followed by stuff with nothing'.

If you want to remove the final comma in a sentence, then that's a bit different - basically, what do you want to do with the string 'one, two, three'? Chop off 'two, three' or leave 'two' there?
#12 at 16:39:17 - 30/01/2009
peej
Flag
Posts:14637
Comments:4691
Thread Kills:462(3%)
AATG Pts:400
Star Rating
Gold Medal
That's the line of thinking I was working to, but it's a series of unequal length strings I'm chopping - Basically their common factor is that there's text after a comma that I want to chop but I can't use a strlen or substr type dealio to chop stuff, as I'm not always able to predict which characters to chop / show

#13 at 16:59:08 - 30/01/2009
mal
Flag
Posts:532
Comments:165
Thread Kills:40(8%)
AATG Pts:85
Star Rating
Why not? The index function deals with variable length strings by finding where the comma is every time you give it a string.
#14 at 18:05:47 - 30/01/2009
eviltobz
Flag
Posts:304
Comments:68
Thread Kills:15(5%)
AATG Pts:80
Star Rating
Bronze Medal
aye, and you can probably do a reverse index if you just want to hit the last instance of a character in a string like "1,2,3,4"
#15 at 19:11:25 - 30/01/2009
Jellyhead
Flag
Posts:943
Comments:35
Thread Kills:38(4%)
AATG Pts:50
Star Rating
Use Split function. It splits a sting into an array of elements based on a delimiter.

I use it all the time to parse input lines from csv files :)
#16 at 21:14:07 - 30/01/2009
peej
Flag
Posts:14637
Comments:4691
Thread Kills:462(3%)
AATG Pts:400
Star Rating
Gold Medal
BINGO that's the kiddie...cheers JH!
#17 at 17:40:30 - 02/02/2009
peej
Flag
Posts:14637
Comments:4691
Thread Kills:462(3%)
AATG Pts:400
Star Rating
Gold Medal
Lack of sleep is really fucking me up but I should know this!

Say a person had to enter 5 numbers into a database record. Is there any way of ensuring that the person hasn't entered 5 numbers the same (ie each number is unique?) without it interfering with all the other records (so using unique key for each field value is out)

?
#18 at 13:45:37 - 05/02/2009
HairyArse
Flag
Posts:6388
Comments:1774
Thread Kills:127(2%)
AATG Pts:350
Star Rating
Gold Medal
Not sure I quite understand your question but can't you just query each entry and if that number already exists spit out an error message? That sounds way too obvious an answer and makes me think I've misunderstood your initial question.
#19 at 14:42:19 - 05/02/2009
peej
Flag
Posts:14637
Comments:4691
Thread Kills:462(3%)
AATG Pts:400
Star Rating
Gold Medal
No that is what I'm doing at the moment, I just thought there might be a neat way of doing it within the database itself so it got all pissy about non-unique entries.

Not to worry, I'll just carry on with the PHP form validation route. Ta.
#20 at 14:49:29 - 05/02/2009
peej
Flag
Posts:14637
Comments:4691
Thread Kills:462(3%)
AATG Pts:400
Star Rating
Gold Medal
Back to those magic 5 numbers again but this time it's comparing 5 fields to ensure that no more than three of the fields have the same value.

So for instance, if a student picked 5 course fields, I want it to squeak if that student's chosen more than three projects from the same course code - ie 5 civils projects instead of 3.

Any ideas? Is there a way to make strcomp work for more than 2 strings?

#21 at 11:50:59 - 19/11/2009
HairyArse
Flag
Posts:6388
Comments:1774
Thread Kills:127(2%)
AATG Pts:350
Star Rating
Gold Medal
The only way I can think to do it is to just compare each string with each other string.

So compare 1 with 2 3 4 5
then 2 with 3 4 5
then 3 with 4 and 5

etc etc.
#22 at 12:00:38 - 19/11/2009
peej
Flag
Posts:14637
Comments:4691
Thread Kills:462(3%)
AATG Pts:400
Star Rating
Gold Medal
Was worried you might say that :) That was my original thought on how to do this. Bums, sounds like I'm going to have a lot of work ahead :(
#23 at 12:01:41 - 19/11/2009
HairyArse
Flag
Posts:6388
Comments:1774
Thread Kills:127(2%)
AATG Pts:350
Star Rating
Gold Medal
The other way of doing it is to limit the choices in the drop down boxes once the maximum number in each category has been selected. But that makes your form a 5 stage submit process.
#24 at 12:11:17 - 19/11/2009
peej
Flag
Posts:14637
Comments:4691
Thread Kills:462(3%)
AATG Pts:400
Star Rating
Gold Medal
Sadly this is a bit more than just stopping 'em choosing less than three of the same course fields as they're held in a separate table.

What I've ended up doing is a comparison matrix that extracts the 5 choice numbers and does a row by row lookup on them for the course codes, then compares all 5 course codes - TWICE (because there are two course codes per choice) just to find out which students didn't bloody listen when they were told not to oversubscribe certain course types.

Fucking nightmare. I'm completely maths blind but this has been one of the most complicated bits of code I've ever written. The bastard works though, and the person who requested it can't quite believe I managed to do it in an afternoon.

There's probably some incredibly simple way to do it that someone with a genius streak would find, but fuck it, it works :)

/polishes stealth_coding_ninja badge.
#25 at 17:30:42 - 19/11/2009

home