Reading the server time instead of the users time - php

I'm using this function to get the date of the last Saturday (The Saturday of this week):
$saturday = strtotime("last Saturday");
The function outputs the date correctly, however when I change the date of my PC, the output returns according to the new date, so is there a way I can force PHP to read the date of the server not the date of the user's PC?

It uses the server date. My guess is that you host your PHP server on the same PC, and this is why you are getting this result.

PHP has no access to the users time. So when you change your time, and the time changes in the browser I think you are hosting the server on localhost.
if you want to check that it takes the servers time, change the server where you host the page

use
time()
if you want it formated use
$now = time();
$formated = date("format", $now);
//for format google php date function

Related

get time and date in php

i am using
date_default_timezone_set('Africa/Nairobi');
$date = date('Y-m-d H:i:s');
but its always returning date and time form my PC.
Ex.. today is - 15/07/2015
and i changed my PC date to 17/09/2016
so php Date also returning same date...(17/09/2016)
is there any why to get real time and date?
what i have tried
1. simple date function
2. set timezone
3. i have searched on google but no luck yet...
It will always return your PC date as it well should.
Date & time functions use the server's date and time. So if you're running a local server (WAMP, XAMP or whatever) your PC will be the server and therefore it's time will be used.
Setting the timezone should change the time accordingly though.
There is nothing wrong with your code
date_default_timezone_set('Africa/Nairobi');
$date = date('Y-m-d H:i:s');
but as #Rizier123 said that if you are using XAMPP or WAMP or any other local server it will show the system time only. I would like to suggest you that put your code to an online server or use some online php compiler, then it will surely gonna give you the expected output.

php code to get the timezone and the difference in current system time with GMT

Please help me with the php code to get the timezone and the difference in current system time with the London time
I need to get the time zone from where the mail is sent.After that i need to get the current system time as well as the GMT difference of current system time in php
get the current system time zone offset
get the the london time zone offset(from mail is sent)
London time zone : date_default_timezone_set('Europe/London');
PHP is server-side, and calling date() will give the time of the server.
You can pass different arguments to date() to get the information you need. date('Z') will give you the timezone offset in seconds (offset from UTC/GMT). You can also get it in hours and minutes using date('P').
To get the timezone of your local machine, you'll have to use something client-side, like Javascript. You could use jsTimezoneDetect to do this.
Give Javascript access to the server date, and compare the two dates:
var server_date = new Date(<?= date("YYYY-MM-DD") ?>);
var local_date = new Date();
You'll need to replace YYYY-MM-DD with a more appropriate time-and-date format string.
This blog and this PHP class should be enough for your needs
Tested function to get timezone difference between UTC and User's Timezone (Asia/Kolkata) can found
TimeZone Difference

PHP date() function not giving correct time

I am trying to figure out why php date() is giving me the wrong time, setting the actual time back 2 hours.
<?php echo date("Y-m-d H:i:s"); ?>
This gives 2011-01-01 03:14:04 instead of 2011-01-01 05:14:04. The hour is decreased by 2.
I have not change my timezone for date() and when users visit the site I want the time to be correct for their timezone also. How can I get this to work using php?
it is because by default it shows GMT time you can change for your region with following code
date_default_timezone_set("Asia/Bangkok");//set you countary name from below timezone list
echo $date = date("Y-m-d H:i:s", time());//now it will show "Asia/Bangkok" or your date time
List of Supported Timezones
http://www.php.net/manual/en/timezones.php
You would have to use either date_default_timezone_set() or a datetime object, and the user would have to set their own timezone in an options menu somewhere.
Otherwise, PHP is a server side language and has no idea what time it is on the user's end.
You would have to use a client side language, JavaScript. You could either have it just be static and display the current user system time, or if for whatever reason you needed to get their time into PHP, you could use some AJAX like scripting to have JavaScript send their time into a script when the page loads.
Try setting the the timezone: date_default_timezone_set or via the ini
Update: you cannot set the correct date for your users. Javascript can handle it but you'd have to rely on the user's system to determine his/her time.
//Change date format
$dateInfo = date_parse_from_format('m-d-Y', $data['post_date']);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
$dateInfo['month'], $dateInfo['day'], $dateInfo['year']
);
$data['post_date']=date('Y-m-d',$unixTimestamp);

Converting UTS (UTC-0) times to BST in PHP or jQuery

I'm pulling the most recent listened tracks from last.fm and putting them on my website.
Problem is, the times are retrieved in UTC-0 uts format and appear to be an hour out when comparing them to BST times in order to calculate a fuzzy time stamp ("about 5mins ago", "about an hour ago" etc).
Is there any way solve this so the times always match BST/GMT and adjust when entering and leaving daylight saving time?
Here's a snippet of PHP code i'm using at the moment, which results in the times being an hour out.
$now = time(); // use this so all times are to the same second
$tz = getenv("TZ"); // save local setting so we can reset it later
putenv("TZ=Europe/London");
$trackPlayedAt = date('d M Y H:i:s', $track->date->uts);
date() automatically formats to the local timezone. The timezone depends on the configuration of the PHP server. If everything is set correctly, it should just work.
If you are running PHP 5.3 you have more options. Comment with which version of PHP you are running.

Problem in DATE AND TIME

I am using date() function it getting the date and time as per my given format but the time its showing me is 4 hours forward than my current local machine time:
This is my code
echo date("Y-m-d h:i:s", time());
Its showing me : 2009-10-28 08:47:42
Where as it should Disply : 2009-10-28 04:47:42
Any Idea whats wrong with this and why its showing different time.
it is likely giving you GMT, you need to set your timezone: e.g. date_default_timezone_set('America/Los_Angeles');
http://php.net/manual/en/function.date-default-timezone-set.php
Make sure your time zone is set correctly:
e.g.
date_default_timezone_set('UTC');
It's returning the timezone of your server, not your computer
try http://www.php.net/manual/en/function.date-default-timezone-set.php
Because you are in, probably, US/Eastern (America/New_York, currently EDT) time zone, but the PHP you are using is running in UTC. You need to ensure that the TZ variable is set in the PHP environment.

Categories