0

I'd like to know if it is possible to subtract a string 24 hour time?

<?php if($_POST[startTime] - $_POST[endTime] == 0) echo 'PLEASE ADJUST YOUR START TIME OR END TIME'; ?> <div class="form-group"> <label class="col-lg-3 control-label" for="inputBday">End Time </label> <div class="col-lg-9"> <select class="form-control" id="endTime" required> <option value="">Select End Time</option> <option value="00:00">12 MN</option> <option value="01:00">1:00 AM</option> <option value="02:00">2:00 AM</option> <!-- and so on .... --> <option value="11:00">11:00 AM</option> <option value="12:00">12:00 NN</option> <option value="13:00">1:00 PM</option> <option value="14:00">2:00 PM</option> <!-- and so on ... --> <option value="22:00">10:00 PM</option> <option value="23:00">11:00 PM</option> </select> </div> </div> 

That's the only way I know how to let the user pick time. I have tried using a certain ready to use classes available on the internet, however I can't use the code twice (one for end and one for start time). I tried testing the first code above, subtracting the strings, it always returns me zero. I think the system just subtracts the number of characters. (i'm not sure though).

Also, I tried changing each option value to normal numbers like from "01:00" to "1", it returns me Parse error: syntax error, unexpected 'die'. (I use die in debugging, like printing the value using die). I even tried removing the quotes so that I think it will be converted to int. Returns me the same error.

0

2 Answers 2

1

If you never need to deal with partial hours, e.g. 03:45 and ONLY full hours, then why not revamp your form to just send the hours value?

<option value="0"> 12:00am <option value="1"> 1:00am ... <option value="15"> 3pm ... 

Then the subtraction becomes trivial.

Sign up to request clarification or add additional context in comments.

5 Comments

same, it returns me this: Failed to run query: SQLSTATE[23000]: Integrity constraint violation: 1048 Le champ 'startTime' ne peut être vide (null) :(
since you didn't mention databases at all in your question, no surprise there.
my database is fine, I think. because it doesn't return the same when the start time and end time fields were just plain text (not drop down list). Question: aren't there any error/warnings in my code above for drop down list?
Ok, I returned my old codes and then place the new codes (drop downlist) in start time and returned the same error. Therefore the error is in the new codes (drop down list code) hmm..
Ok, I got the answer. 1. I don't have the name="", instead I have id="". 2. missing div tags. Thanks for the answer though :) thank you. I appreciate it. I'm displaying the selected time of the user, so I think I have to think of a way where the value will have a PM and an AM.
1

You need to convert the string time using strtotime - and then compute the difference between the two:

$startTime = strtotime($_POST['startTime']); $endTime = strtotime($_POST['endTime']); if($endTime - $startTime <= 0) { // end time selected is before or the same as start time } 

The difference you get will be in seconds.

And, naturally, you need to add error checking to it. What if you receive abcd and xyz as your start and end time strings?

4 Comments

ok wait. first: what's the difference between $_POST[startTime] and $_POST['startTime']??? I do not include single quotes ALWAYS and im 99% complete in my project. Tell me it's okay to not include single quotes pls?
@GlennVon Single or double quotes - doesn't matter - but you should include them. Without the quotations, you'll see warnings (if you turn on your error display) about unknown constants. The fact that it may work for you now is irrelevant.
it works for me (not putting the single quotes), so I won't mind it now. Anyways, am I doing it right? my codes is like this: <option value="00:00">12 MN</option> will that be converted in to time? or should I use <option value=0>.... or <option value="0".... ??? I tried outputting the $startTime = strtotime($_POST['startTime']); by echoing. It returns me this: Failed to run query: SQLSTATE[23000]: Integrity constraint violation: 1048 Le champ 'startTime' ne peut être vide (null)
Ok, I got the answer. 1. I don't have the name="", instead I have id="". 2. missing div tags. Thanks for the answer, I'll mark it later as an answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.