So basically, What I wanted to happen was, I wanted to submit an article which submits my current time() and the amount of hours until a certain event. I wished for it to calculate the amount of time in hours from the time() in the database to the amount of hours since that article was posted.
So if I posted an article for an event happening at 3pm, and the current time is 12pm, It would say "Event in 3 hours". And if I were to check again at 2pm, It would say "Event in 1 hours".
Sorry if this seems like a noob question, But I am quite the stupid person when it comes to maths and stuff.
Thanks in advance for any input, I appreciate it!
Presuming you store the time of the article as a timestamp, and you retrieve the value in the variable $articleTime:
$hoursLeft = floor(($articleTime - time()) / 3600);
Related
I saw some website that after you have registered, you can use the service like posting a comment immediately.
You may have to wait for 5 or 10 minutes to be able to start, like this StackOverflow website.
Once you have asked question, you have to wait 20 minutes to select your answer.
If I want to do it in JavaScript or PHP can anyone show me how to do it? I assume you have to compare the time with current time-stamp, but don't know how to exactly implement it. Thanks in advance!
Assuming you have a datetime string stored in a variable, you could do something like:
// TODO: get a datetime string into $time
$minutes = 5;
if (strtotime($time) + $minutes * 60 < time()) {
// It's been 5 minutes since $time
}
In PHP:
$wait_time = 10*60; //10 minutes * 60 seconds
if(time() > $start_time + $wait_time) {
}
sure, you'll want to compare the date of posting, with the current date, but then formulate the conditions that will validate a new post or the selection of the same.
Each question that is posted will require server side storage, if not exclusively in a db then there will need to be at least a record in a table linking the question to the user.
Once a question has been posted you could either have the server side script that loads the question carry out the following tasks.
Get the question entry
Is the person that posted the question the current requesting user
[yes]
Is the request time minus the post time greater than (number of mins * 60)
[yes]
Include select answer buttons (html objects or buttons)
Alternatively you could always show the buttons and carry out the above logic and return some indication that the user must wait x minutes before they can select an answer.
You have to store the creation time (of whatever object or saving) along with the item being created in the database. So for example, if you create a question on SO, they probably store the time you posted the question. Then when you go to view your question, they probably get the difference between the current time and the time stored.
So say that you want to implement a 20 minute wait after posting a question before you can select an answer. Then you upload a question at 5:00 PM. When you attempt to view your question at 5:05PM the same day, the difference in time is 5:05PM - 5:00PM, which is 5 minutes. Take 20 minutes - 5 minutes to get the time the user must wait, and send that back to the page.
From javascript you can get that time limit (15min), and create a setTimeout method to "unlock" the answer feature. The code would look something like this:
<input type="hidden" id="waitfor" value="15">
$(document).ready(function () {
var waitfor = $('#waitfor').val();
timer = setTimeout(function () {
// let the user know they can post an answer
}, waitfor * 60 * 1000);
});
Also, I'd let the user post an answer whenever they'd like, but prevent it being submitted on the client side if it hasn't been 20 minutes.
Is there a way to add articles (data) in mysql/php but make them
auto-publish during the day when I´m not available.
So lets say, if I have a news site but I´ll be busy tomorrow the whole day so I could pre-write articles the day before with timestamp and they would appear when I want
Is this possible?
How would the script be like:
SELECT FROM articles WHEN TIME is 2011-12-01 12:15
Thanks
As simple as:
SELECT * FROM articles WHERE timestamp <= NOW()
Though I never worked with them to me the easiest solutions seems to be Cronjobs combined with an extra waiting table and a script linking both.
You pre-write your article and store them in table together with the time stamp you want to publish them.
Your cron will invoke a script every 2,3, 5 hours (twice a day, whatever).
This script checks the time stamps in the table against the actual time and if it is about time to realise the article it will do so (or hand the information to the realise script).
How to get how many hour minute seconds laps on message on php mysql?
like
23 seconds ago
1 minute ago
is this possible?
A really simple way is using the jQuery plugin timeago http://timeago.yarp.com/
The neat thing about it is, that it updates the entries while viewing the page :)
In db table create field time INT(10). Insert in this filed php time().
In script check diff:
$diff = time() - $time_from_db;
echo $diff . 'seconds ago'.
If you did want to have the data fed from the database you could write SQL like this:
SELECT FLOOR((UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(your_date)) / 86400) as days FROM your_table
(MySQL example)
You could should then repeat for hours, minutes etc. subtracting the reminder.
From your result you would have $result['days'] available.
This said I would go for the timeago solution mentioned by Tim. But it is hard to say not knowing your application.
In my DB I stored the time of the last page refresh in a TIME column. I want to compare the last page refresh to the current time now and find out how many 5 minuet periods have passed?
I was thinking take the last page refresh and subtract it from the current time then divide by 5. But I don't know about how things are formatted.
Help me please!
SELECT *, FLOOR(((UNIX_TIMESTAMP()-time_field)/300)) AS periods FROM table
Im not sure for if UNIX_TIMESTAMP() returns correct timestamp, you will have to check it ;)
EDIT: checked it myself and its working great ;)
I've got two times that need to be stored into a database. The time format is hh:mm:ss with NO DATE. These times can be changed by the users on the system. One is a time on and the other is a time off. The time off should always be greater than the time on within a 24 hour cycle.
Here is the part that I'm having trouble with. I don't want to limit the user to selecting times before midnight to keep everything in the same "daily" cycle so I'd like to be able to logically determine if the users' times are simply within a 24 hour time period and then test that the on time is always less.
Can someone help me work through this? There are so many time and date functions that I really don't know which one(s) I need to do this; plus, I'm unclear on how I should test for this.
I'm starting to think that there is no way to test for this without having a date included. Just the times is not enough.
The time is always within a 24 hour cycle, so if the user puts 01:00/03:00 he's on for 2 hours
If he writes 03:00/01:00 he's on for 22 hours.
I dont see the problem.
The OP wrote in a comment:
The user can opt to get a report
delivered in a window of time. The
user may opt to have their reports
delivered in a window from 23:00:00 to
01:00:00 hours. They may decide
tomorrow that that time is no longer
good and change it to 23:0:00 to
05:00:00 or something like that. Am I
missing something??
You have no problem in the time definition part. You may want to play with the code that sends out the report.
// current time
$timeNow = time();
// fetch user time options from database
$timeOn = [from the database];
$timeOff = [from the database];
// convert times to seconds from epoch
$timeOn = strtotime($timeOn);
$timeOff = strtotime($timeOff);
// if database time is in timestamp format,
// only the hour, minutes and second information is needed
$timeOn = mktime(date("H", $timeOn), date("i", $timeOn), date("s", $timeOn));
$timeOff = mktime(date("H", $timeOff), date("i", $timeOff), date("s", $timeOff));
// if time on is higher than time off, time on is of yesterday
if($timeOn > $timeOff){
$timeOn = strtotime("-24 hour", $timeOn);
}
// decide on report sending
if($timeNow >= $timeOn && $timeNow <= $timeOff){
// Send report
} else {
// Do not send report or reschedule the report
}
Any two times in hh:mm:ss format are going to be within a 24 hour time period, as you state. So unless you actually store a date, I am not sure how you can do this.
If I understand correctly, a start time of 23:00:00 and an end time of 04:00:00 should be acceptable to you (this just means 5 hour work shift)? If this is acceptable, then can you give me an example of unacceptable input?
Perhaps you want to check that the end time is within 12 hours of the start time? That should be feasible.