How does PHP date() function work? - php

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

Related

Time Format for Filemaker PHP

I'm having an issue with the FileMaker PHP API. I don't know how to format a time correctly for input into a Time field.
I tried entering a string with the format that the documentation guide says (H:M where H can be 24 hours) and it still doesn't take. I don't know if I'm supposed to change the data type with another function like date() or what.
Edit: A little snippet of code
//Date
if($arrDate!=NULL){
$booking->setField('arrival_date', '07/17/2015');
}
//Time
if($arrTime!=NULL){
$booking->setField('arrival_time', '11:00:00');
}
$savedBooking = $booking->commit();
I replaced the variables for the value fields with what they should be just in case it was something going wrong with them, but it still doesn't work. The date field setting works fine if I comment out the Time section however.
Here's the format that I use:
$booking->setField('arrival_date', date ( "n/j/Y H:i:s A" ) );
If you're interested in adjusting the time, here's a post to checkout: http://timdietrich.me/blog/php-timezone-arithmetic/
Good luck!
I suspect you may have some validation set up on the "arrival_time" time field. What is the error that is returned by the API? It should return an error code that will tell you more.
Also worth mentioning that FileMaker will inherit the date and time settings from your system settings. For example, date formats are different in Europe than they are in the states. But that time value you give looks like it would work, and you only get an error when setting that field, so I would look to make sure the field is defined correctly.
You can also test by putting that value in via FM Pro and see what happens.
I'm an idiot, after hours struggling with this I realized I had put the wrong field name! Thanks for your help guys. A string of H:M worked perfectly.

how to set date and time not depending on computer date and time?

Please help,I want to set my webpage date not based on computer time because what if the user's time is not set correctly. i have tried searching in google but unfortunately did not get my answer.
I tried this code but when i change my pc date and time it also changes the output in my webpage to my pc time.
<?php
date_default_timezone_set('Asia/Manila');
$timezone = date_default_timezone_get();
echo date("Y/m/d H:i:s");
?>
This would hardly be a problem, as the website code is run on the website server, which has its very own time and date setting. As long as you make sure the date/time setting of the server is correct you will be fine.
The date/time setting of visiting users sure can differ a lot, depending on the user's geographical location. It may cause trouble when running JavaScript on a webpage, but for server code (e.g. PHP) it would be irrelevant.
The date will be set relative to the server that runs the code, if you run your code with a server instance on your computer, changing your computer date settings will afect your script but if you use a server wich is not on your computer the date setting that will be returned by your script will be the setting of your server not your computer's.

Working with unix timestamps and timezones

Timezones and timestamps confuses me so I'm hoping someone can answer my questions :)
Lets say I have a Python script that parses an RSS feed, converts the date value into a timestamp using the following code and stores it in a database:
article_date = parse(article.published).strftime('%s') if hasattr(article, 'published') else round(time.time())
Now when I retrieve that record from the db in PHP, and I run the following code, does PHP assume the timestamp was UTC-0 and automatically offsets the timezone to Eastern time?
date_default_timezone_set('America/New_York');
echo date('Y-m-d H:i:s',$timestamp);
I'm seeing weird issues with my dates, so I'm wondering if someone can help me out with advice on how to properly convert and store rss feed timestamps. I can across this line of code somewhere so should I put this at the top of my script?
os.environ['TZ'] = 'Europe/London'
If you want to set your timezones and keep them aligned in PHP and in Python, then your PHP code is completely correct and for python you need to apply the following:
os.environ['TZ'] = 'America/New_York'
time.tzset()
before you call strftime()
That should make sure you store the time in the same zone you're trying to retrieve it.
Note: tzset() is a Unix-only function.

Open page at specific time

I would like to open access to a page at specific time frame in php.
Say for E.g 9:00AM to 12:00PM - User will be able view a page after which they would get you are not allowed to view this page at this time.
Is this possible in PHP without accessing database?
If so can somebody guide me?
Thanks!
You can do something like this:
if(date('G')>=9 && date('G')<=11)
{
// show your code/site/content.
}
else
{
// Show the "Come back during opening hours..." sign.
}
The date function by default uses the specific time and using G gives a 0-24 value for the hour of the day (no leading zeroes).
This will use the server time though - which is what I expect you want rather than using the user's timezone if they are not at the same timezone as your server.
If your server is in a different timezone to what you expect your users to be at, use date-default-timezone-set to set it to where you want.
You'd better use .htaccess directives. The idea is described here http://www.blog.highub.com/apache/http-server/htaccess-deny-diractory-access-during-a-specific-time/

want to show a time for the comment last posted through php

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

Categories