I want to retrieve date and time from server and according to it do some thing. For this I used following code:
$info = getdate();
$date = $info['mday'];
$month = $info['mon'];
$year = $info['year'];
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];
$current_date = "$date/$month/$year == $hour:$min:$sec";
This code returns proper date but problem is that what I see in my cpanel(server time) is diff. than what I get from code. In cpanel time is CDT while from code it is showing UTC which I reconfirm using following code
<?php echo date("r == e"); ?>
Why this is happening and what changes I have to do in my code so that I can get proper server time.
You should set the timezone to the one of the timezones you want.
// set default timezone
date_default_timezone_set('America/Chicago'); // CDT
$info = getdate();
$date = $info['mday'];
$month = $info['mon'];
$year = $info['year'];
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];
$current_date = "$date/$month/$year == $hour:$min:$sec";
Or a much shorter version:
// set default timezone
date_default_timezone_set('America/Chicago'); // CDT
$current_date = date('d/m/Y == H:i:s');
Try this -
<?php
date_default_timezone_set('Asia/Kolkata');
$timestamp = time();
$date_time = date("d-m-Y (D) H:i:s", $timestamp);
echo "Current date and local time on this server is $date_time";
?>
No need to use date_default_timezone_set for the whole script, just specify the timezone you want with a DateTime object:
$now = new DateTime(null, new DateTimeZone('America/New_York'));
$now->setTimezone(new DateTimeZone('Europe/London')); // Another way
echo $now->format("Y-m-d\TH:i:sO"); // something like "2015-02-11T06:16:47+0100" (ISO 8601)
You have to set the timezone, cf http://www.php.net/manual/en/book.datetime.php
For enable PHP Extension intl , follow the Steps..
Open the xampp/php/php.ini file in any editor.
Search ";extension=php_intl.dll"
kindly remove the starting semicolon ( ; ) Like : ;extension=php_intl.dll. to.
extension=php_intl.dll.
Save the xampp/php/php.ini file.
Restart your xampp/wamp.
You should set the timezone to the one of the timezones you want. let set the Indian timezone
// set default timezone
date_default_timezone_set('Asia/Kolkata');
$info = getdate();
$date = $info['mday'];
$month = $info['mon'];
$year = $info['year'];
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];
$current_date = "$date/$month/$year == $hour:$min:$sec";
You can use the "system( string $command[, int &$return_var] ) : string" function. For Windows system( "time" );, system( "time > output.txt" ); to put the response in the output.txt file. If you plan on deploying your website on someone else's server, then you may not want to hardcode the server time, as the server may move to an unknown timezone. Then it may be best to set the default time zone in the php.ini file. I use Hostinger and they allow you to configure that php.ini value.
Related
I am trying to convert date time into UTC, on my previous project it was working and giving the correct DateTime as per requirement but when I run the same code on PHP 8.1 I got a different time!
Previous working code (PHP 5.6)-
function convertDateWithTimeZone($src_tz, $dest_tz, $src_date, $date_format = "Y-m-d H:i:s")
{
$dest_date = "";
$curr_tz = date_default_timezone_get();
date_default_timezone_set($src_tz) or die("Can not change the default time zone");
$strtotime = strtotime($src_date);
date_default_timezone_set($dest_tz) or die("Can not change the default time zone");
$dest_date = date($date_format, $strtotime);
date_default_timezone_set($curr_tz) or die("Can not change the default time zone");
return $dest_date;
}
$startdate = "2023-02-11 00:00:00";
$enddate = "2023-02-14 23:59:59";
$fromdate = convertDateWithTimeZone("Asia/Kolkata", "UTC", $startdate, $date_format = "Y-m-d H:i:s");
$todate = convertDateWithTimeZone("Asia/Kolkata", "UTC", $enddate, $date_format = "Y-m-d H:i:s");
I got the result for strtotime($fromdate) is 1676034000.
Now, when I run the same code in PHP 8 I got a different result -
I got the result for strtotime($fromdate) is 1676053800.
I have a contract time format like this one July 28, 2020 14:18:25 that I want check if it has expired from now. But am not getting is correct. it seems to be some minutes difference
here is the code
//$now = strtotime(date("m-d-y H:i:s"));
$now = time();
$contractDate = strtotime("July 28, 2020 14:18:25");
// check if contract is still on or has ended from now
if($now < $contractDate) {
echo "contract is on";
} else {
echo "contract time ended!";
}
Could it be the timezone problem (that your time() is off by some).
You solve it by setting the timezone explicitly in your PHP scripts. You can do this with date_default_timezone_set():
date_default_timezone_set('America/Los_Angeles');
Here is the list of PHP supported timezones.
You may also want to try a test script calling date_default_timezone_get() to see what it's actually set to to verify that this is in fact the problem.
You can also try with DateTime class
$d1 = new DateTime();
$d2 = new DateTime("July 28, 2020 14:18:25");
$diff = $d1->diff($d2);
echo $diff->format('%H:%I:%S');
You are not able to get it right because of your format. It should work if you can get your dates to be in the "yyyy-mm-dd" format. Try this:
$now = strtotime(date("y-m-d"));
$now = time();
$contractDate = strtotime("2020-07-28");
if($now < $contractDate) {
echo "contract is on";
} else {
echo "contract time ended!";
}
I'm upload the excel data into mysql. in there date save like 16.5.59(dd.mm.yy) formate. I try to change this using this code
$date = '16.5.59';
echo date('d-m-y',strtotime($date));
it always show current time like 04-02-15.
Pls change this to dd-mm-yyyy formate.
Thanks in Advance.
Timestamp was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).
If it not cover the between given range then it will take current date.
$date = '16.5.59';
$date_array = explode(".",$date);
$var_day = $date_array[0];
$var_month = $date_array[1];
> Blockquote
$var_year = $date_array[2];
echo $new_date_format = "$var_day-$var_month-$var_year";
Try to make the date yourself using explode and strtotime:
date_default_timezone_set('America/Los_Angeles');
$date = '16.5.59';
$date_time = strtotime(implode('-', array_reverse(explode('.', $date))))
$date_str = date('d-m-Y', $date_time);
echo $date_str;
Output: 16-5-2059
You ARE required to specify that the year is 1959 instead of 2059 (or are you sure that it's really 2059?)
You can make change from 2059 to 1959 like this:
date_default_timezone_set('America/Los_Angeles');
$date = '16.5.59';
$date_arr = array_reverse(explode('.', $date))
# This line does the job. Make sure all years are between 1900 - 1999.
$date_arr[0] = '19' . $date_arr[0];
$date_time = strtotime(implode('-', $date_arr))
$date_str = date('d-m-Y', $date_time);
echo $date_str;
I hope this answer can help you.
This is by no means the best solution but using php's date_parse_from_format is possible. You could supply a format and use the following
$date = "16.5.59";
$dateObj = date_parse_from_format("j.n.yy", $date)
with the $dateObj you can work what you need such as :
echo $dateObj['year'];
IDE Running Example
Worth noting you require PHP >=v5.3
You can do this if it is for simpler task. It will consume time.
$date = '16.5.59';
$dtSplit=explode(".",$date);
echo "<br>".$dtSplit[0].".".$dtSplit[1].".".$dtSplit[2];
echo "<br>".$dtSplit[0].".".$dtSplit[2].".".$dtSplit[1];
echo "<br>".$dtSplit[1].".".$dtSplit[0].".".$dtSplit[2];
echo "<br>".$dtSplit[1].".".$dtSplit[2].".".$dtSplit[0];
echo "<br>".$dtSplit[2].".".$dtSplit[0].".".$dtSplit[1];
echo "<br>".$dtSplit[2].".".$dtSplit[1].".".$dtSplit[0];
I have this weird situation.
I'm trying to convert my time stored on a table to time-ago format.
It's working well on my localhost, but not on the server.
For some reason, when calculating the difference between the now and the time on the server, I receive a negative number.
Here is an example:
$time = '2015-01-02 05:52:49'; //Time that is stored on the created cell
$now = time();
$seconds = strtotime($time);
$difference = $now - $seconds;
The output for the above code is -13628.
Timezone is set to date_default_timezone_set('America/New_York');.
What am I missing here?
if the date/time you run this is before '2015-01-02 05:52:49' then the - sign is logical. Try use this instead:
$date1 = new DateTime();
$date2 = new DateTime('2015-01-02 05:52:49');
$now = $date1->getTimestamp();
$seconds = $date2->getTimestamp();
$difference = $now - $seconds;
[SOLVED]
So, the problem was that I've set the timezone to date_default_timezone_set('America/New_York');.
To solve this, I changes this to date_default_timezone_set('UTC');
And now it's working well.
User following code
date_default_timezone_set('UTC'):
instead of
date_default_timezone_set('America/New_York');
Example, I have time zone is : "Europe/London" or "America/Los_Angeles"
In Perl, I can look up for Local Time by:
use DateTime;
use DateTime::TimeZone;
$timeZoneName = "MY TIME ZONE HERE";
$timeZone = DateTime::TimeZone->new(name => $timeZoneName);
$timeNow = time();
$dateTime = DateTime->from_epoch(epoch => $timeNow);
$offset = $timeZone->offset_for_datetime($dateTime);
$localTime = $timeNow + $offset;
print "$localTime";
But I don't know how to look up local time with PHP, anyone help?
You can do this in PHP:
$timezone = 'Europe/London'; //perl: $timeZoneName = "MY TIME ZONE HERE";
$date = new DateTime('now', new DateTimeZone($timezone));
$localtime = $date->format('h:i:s a');
echo "Local time is $localtime.";
If you don't have PHP 5.2, see also date_default_timezone_set() or use ini_set('date.timezone', $timezone); and then PHP will operate as if it were in that timezone. Any date you output will be in that of the given timezone.
PHP Date/Time Functions also have good references. There are other ways you could do this in PHP but the above is simple and most servers should have PHP 5.2 or greater.
See date() for a list of formats you can pass to DateTime::format and also the DateTime Class for references on other things you can do with DateTime.