I'm learning PHP.
I have online classes on Zoom at 11:00, 15:00, and 19:00 BST which is UT+1, but I need to display on a (WordPress) webpage what times they are in the student's local time.
In other words, set up a web page to query their browser to get their timezone offset and add that to my local class times so they can display in their timezone's times.
You can't do that from PHP. To get the user's local time, use JavaScript instead of PHP.
const d = new Date();
let years = Math.round(d.getTime() / year);
PHP only gives back the server's time.
I'm just wondering how the php function date() works? Like how does it determine time and date to return?
For example if my php code on a page looked something like this:
<?php
echo date("h:i:sa");
?>
It will simply echo out (for example) 11:18:24am, but let's say a site visitor from another country visits page, will the time returned be appropriate for their timezone? Hopefully this question makes sense, i only ask because I couldn't find anything on Google when searching how php date() function works.
The date method is documented here: https://secure.php.net/manual/function.date.php
As php scripts are executed on server side, it uses the current date/time of the server, formats and returns it. - There is no conversions for the timezone of visitors of your website. If you need localized times, you need to change the timezone manually.
I had the same problem but here is how i solved it. In the php.ini the timezone is static and set according to the server but you can override this by using date_default_time_zone function. for example you can create a script that tests a visitor location then loads parameters to date_default_timezone_set("continent/city"); according to your visitor location. i hope this works
I'm trying to calculate the average rainfall on a tuesday in 2014 so far vs other days.
http://www.knmi.nl/klimatologie/daggegevens/index.cgi has all the information needed, the only problem is that each day can not be accessed with a direct link. An on-screen form has to be used to select a day.
Is there a way to automatically download these pages or the information on these pages with php?
EDIT: to be clear: I need all days from 2014, a day can be selected using the on-screen form.
I have no experience with cgi whatsoever.
Yes there is a way and it has nothing to do with cgi. Regardless of the programming language used to build the site; realistically you are only interested in the output of the page, which is going to tbe html in this case.
Use:
$site_html = file_get_contents('http://www.knmi.nl/klimatologie/daggegevens/index.cgi');
Now $site_html variable has the html output of the given page; you just need to parse it to get the values you want. I believe this is known as scraping web page.
I am having an add/edit form to update and add to database, and I was not sure what the best way is to input TIME type (HH:MM:SS). Should I use multiple html text inputs for HH, MM, SS?
if so, is there a function that prepares the string for database input?
Basically what I'm trying to input is how many hours, minutes, seconds a specific task took to finish.
Can anyone point me in the right direction here?
I'm designing a website using Codeigniter (PHP).
Thanks
Let me go ahead and clarify what needs to happen a bit more...
The user is required to enter data specific to sports more in particular to a players minutes and seconds played. I'm thinking of maybe simplifying it to only minutes. Perhaps this way input is only 1 thing. Then again my question is, what method would work to convert this "minute number" to the correct MYSQL TIME format?
I wrote a helper to do something similar in an app I'm working on. Mine generates three dropdowns, hh, mm and am/pm, by calling built in form_dropdown helper. Once I get the data from the drop downs, I convert it 24hr format and then I just concatenate the strings into the right format for MySQL. Since it's a helper I can just call it from any view using form_time(). I can post it here if you think it would help to see it.
Dana
just use now() function, if your DB is
MYSQL is you want to save the current
time
e.g:
UPDATE tbl SET timemodified = NOW()
and make sure that timemodified has a
type of "time"
Ow sory miss that point. uhm maybe you need to have a start time in your DB, then after he/.she is finish his/her task. you must query on the DB the start time subtract it to the end time(your current time) then the result would be the time he/she performed the task
this checks for 2 numbers, then a ":" then 2 numbers, then a ":", then finally 2 numbers again:
$cleanTime = preg_match( '/(\d\d)\:(\d\d)\:(\d\d)/', $_POST[ 'NAME_OF_TIME_INPUT' ] );
if( !$cleanTime ){ /* ... error ... */ }
don't be scared of all the slashes, haha (I was at first when I used regexps).
replace NAME_OF_TIME_INPUT with the content of the name attribute on the <input on the <form page
i.e. if <input name="coolinput" /> then use $_POST[ 'coolinput' ]
This is one of the eternal struggles of (web) UI design, how to input time without driving the users nuts. What works for your specific case is something only you can decide, because it depends on the exact format/circumstances you need and your target audience.
As general guidelines I'd say:
Don't do a free-form text field that requires a certain format, e.g. "Enter time (HH:MM:SS)", because it's too easy to mess up and will deny the users input or mess up the time if you do no validation.
Try to avoid [0-23] [0-59] [0-59] dropdowns, since they can be quite a pain (click, scroll, click, click, scroll, click, click, scroll, click).
If ease of use is a high priority, as would be the case for public websites, maybe a Javascript enhanced timepicker is a good idea. Try not to use anything too fancy that nobody gets though (like dragging the hands on a clock).
A free-form, free-format text field might be the best idea. The user can just type in "3pm", "16:34" or "midnight". You may need to provide examples to get users started, otherwise they may feel lost. You can run this through strtotime on your end, but you may need to fill in the blanks and do a lot of validation.
Three short text fields may be a good idea if your audience is very keyboard focused and can be expected to tab through them in rapid order.
As for formatting it for SQL, however you receive the time input from the user, you should assemble it to a UNIX timestamp and format that timestamp for SQL:
date('Y-m-d H:i:s', $timestamp);
I have searched for some alternatives and solutions and I came up with this:
$min = 60;
$time[] = floor($min/60);
$time[] = $min%60;
And I used the following to convert to MySQL TIME format
INSERT INTO table (min) VALUES (MAKETIME($time[0], $time[1], 0))
I have had the same issue with working with the html time input. However, I've managed to work around it with a PHP function.
What the function does is translate the time into a format the MYSQL DATETIME datatype can understand.
Of course you will need to enter the date somehow, but I'll leave that up to you.
functions.php
function convertHtmlTime($date,$time){
$newDate = date($date);
$newTime = date($time);
$datetime = new DateTime($newDate.$newTime);
return date_format($datetime, 'YmdHis');
}
test.php
$date="2007-02-16";
$time="23:59";
echo convertHtmlTime($date,$time);
Results:
20070216235900
I want to show when the comment last posted in PHP. like 5 minutes ago, 2 days ago, 7 weeks ago. How to do this?
You can find plenty of answers with full solutions in different languages, pseudocode, ideas, etc.. here.
I believe there's an example of PHP too.
You can use timeago, a jQuery plugin, to do it via Javascript. It would yield the same result, update without refreshing, and by doing it client side instead of server side, you are not precluded from caching.
http://timeago.yarp.com/
Otherwise, Annurag has a link with some good PHP solutions.
You can do manual calculation in server, to get the time difference, then translate it into human time format.
Or my preference, do it in browser using javascript. Using this approach, the time in page can be updated without refresing the page.
You can use this jQuery EasyDate library to translate a DOM element into humane time format.
You can also read the comments in this post about Pretty Date by John Resig. It contain the code, and improvement by other.
Store the comment posted in the date DB and show the the same in the front end by comparing with current date and time using php function