Get South Africa Standard Time In Php - php

this function is used to get current time of my system, I want to get South Africa time, so plz guide me.
$today = time () ;

For example:
<?php
date_default_timezone_set('Africa/Johannesburg');
echo date('Y-m-d H:i:s', time());

$d = new DateTime("now", new DateTimeZone("Africa/Johannesburg"));
echo $d->format("r");
gives
Mon, 07 Jun 2010 02:02:12 +0200
You can change the format. See http://www.php.net/manual/en/function.date.php
time() gives the number of seconds since January 1 1970 00:00:00 GMT (excluding leap seconds), so it doesn't depend on the timezone.
EDIT: For a countdown, you can do:
$tz = new DateTimeZone("Africa/Johannesburg");
$now = new DateTime("now", $tz);
$start = new DateTime("2010-06-11 16:00:00", $tz);
$diff = $start->diff($now);
echo "Days: " . $diff->format("%d") . "\n";
echo "Hours: " . $diff->format("%h") . "\n";
echo "Minutes: " . $diff->format("%i") . "\n";
echo "Seconds: " . $diff->format("%s") . "\n";

The time() function returns the same value all around the world. Its return value is not dependent on the local time zone.
To convert a time value to your local time, call the localtime() function.

Related

Date say that we are 1st april and forget 29th,30th,31st

I'm working with some dates in PHP, and the problem is that today, php return me that we are the 1st of April.
I don't really know why
Tried to change the code to get the date.
$actArray= array();
for($i=0;$i<30;$i++){
$date = new DateTime(date("Y-m-d"));
$date->modify("-".$i." days");
$date->modify("-1 months");
$actArray[date("Y-m-d",time()-60*60*24*$i)]=array("display"=>$date->format("Y, m, d"),"MaxPlayers"=>0,"PlayersOn"=>0,"Register"=>0);
}
Alerdy tried
for($i=0;$i<30;$i++){
$date = new DateTime(date("Y-m-d",time()-60*60*24*$i));
$date->sub(new DateInterval("P1M"));
$actArray[date("Y-m-d",time()-60*60*24*$i)]=array("display"=>$date->format("Y, m, d"),"MaxPlayers"=>0,"PlayersOn"=>0,"Register"=>0);
}
Same result
It seem that he skip 3 days.
Output : 2019, 03, 01 2019, 02, 28 2019, 02, 27 2019, 02, 26 2019, 02, 25 2019.....2019, 02, 28
Thanks for you help
Why don't you use date() function? It returns the local time/date.
you can get today's date:
echo date("Y, M, d");
for more details: date function php
https://www.guru99.com/php-date-functions.html good reference
Not every month has 30 days, some have even less.
Not all days have 24 hours.
DateTime / strtotime has so many options, you'll never need to do fancy time calculations yourself.
Consider this code:
$today = new DateTime('today');
echo $today->format('Y-m-d') . PHP_EOL;
$today->sub(DateInterval::createFromDateString('30 days'));
echo $today->format('Y-m-d') . PHP_EOL;
$today = new DateTime('today');
echo $today->format('Y-m-d') . PHP_EOL;
$today->sub(DateInterval::createFromDateString('1 month'));
echo $today->format('Y-m-d') . PHP_EOL;
Output:
2019-03-29
2019-02-27
2019-03-29
2019-03-01
You see, there is a difference between "30 days" and "1 month"...
Another example:
$today = new DateTime('today');
echo $today->format('Y-m-d') . PHP_EOL;
for($i = 0; $i < 5; $i ++) {
$today->add(DateInterval::createFromDateString('1 days'));
echo $today->format('Y-m-d') . PHP_EOL;
}
2019-03-29
2019-03-30
2019-03-31
2019-04-01
2019-04-02
2019-04-03

PHP DateTime operations - set new timezone and deduct 24 hours

I am having a surprisingly hard time with the datetime() statement in PHP. What I am trying to achieve is to read the current server time (located in GMT-5), based on that time calculate the user specific date and time as per timezone, and then deduct 24 hours from the users current time. See an example below for users in London and Shanghai:
$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;
echo "<hr>";
$server_datetime = new \DateTime('now'); //located in America/New_York timezone - GMT-5
$server_newdate = (new \DateTime())->modify('-1 day');
echo "Current Server DateTime: " . $server_datetime->format('Y-m-d H:i:s') . "<br>";
echo "If you deduct 24 hours you get " . $server_newdate->format('Y-m-d H:i:s');
echo "<hr>";
$usersTimezone1 = 'Europe/London'; // GMT-0
$user_datetime1 = $server_datetime->setTimezone(new DateTimeZone($usersTimezone1));
$user_newdate1 = ($server_datetime->setTimezone(new DateTimeZone($usersTimezone1))->modify('-1 day'));
//$user_newdate1 = $user_datetime1->modify('-1 day');
echo "London User DateTime: " . $user_datetime1->format('Y-m-d H:i:s') . "<br>";
echo "If you deduct 24 hours you get " . $user_newdate1->format('Y-m-d H:i:s');
echo "<hr>";
$usersTimezone2 = 'Asia/Shanghai'; // GMT+8
$user_datetime2 = $server_datetime->setTimezone(new DateTimeZone($usersTimezone2));
$user_newdate2 = $user_datetime1->modify('-1 day');
echo "Shanghai User DateTime: " . $user_datetime2->format('Y-m-d H:i:s') . "<br>";
echo "If you deduct 24 hours you get " . $user_newdate2->format('Y-m-d H:i:s');
As you can see, the problem is that I cannot deduct the 24 hours directly from the $user_datetime variable which holds the current time of the user's timezone. Neither deducting from the variable directly nor creating a new instance of datetime() for this timezone seems to do the trick.
// attempt 1 - not working
$user_datetime1 = $server_datetime->setTimezone(new DateTimeZone($usersTimezone1));
$user_newdate1 = ($server_datetime->setTimezone(new DateTimeZone($usersTimezone1))->modify('-1 day'));
// attempt 2 - not working either
$user_datetime2 = $server_datetime->setTimezone(new DateTimeZone($usersTimezone2));
$user_newdate2 = $user_datetime1->modify('-1 day');
As soon as I start modifying the users datetime, the script will only return the current date and time of the users timezone, nothing else.
Thanks for the help
As a sort of best practice, you can use this:
$serverDateTime = new DateTime();
$userTimezone = new DateTimeZone('Europe/London');
$userDateTime = $serverDateTime->setTimezone($userTimezone);
$dateInterval = new DateInterval('P1D');
echo $userDateTime->sub($dateInterval)->format('Y-m-d H:i:s');
But if you want to subtract directly from the variable, you can use this one:
$serverDateTime = new DateTime();
$userTimezone = new DateTimeZone('Europe/London');
$userDateTime = $serverDateTime->setTimezone($userTimezone)->sub(new DateInterval('P1D'))->format('Y-m-d H:i:s');
echo $userDateTime;
//Asia/Kolkata +5:30
date_default_timezone_set("Asia/Kolkata");//set time zone
$istTime= strtotime(date("d-m-Y h:i:s"));//get timestamp in seconds
echo date("d-m-Y h:i:s");//16-03-2017 09:44:52
//Europe/London +0:00
date_default_timezone_set("Europe/London");//set new time zone
$dayBeforeTime= strtotime(date("d-m-Y h:i:s",$istTime))-86400;//get timestamp in seconds and subtracting 1 day seconds
echo date("d-m-Y h:i:s",$dayBeforeTime);//15-03-2017 04:14:52

PHP Date not sticking

I'm having a hard time getting dates to stick in variables. When I when I'm tyring to subtract months from a current date it is giving me 0 back (defaulting back to 1-1-1970).
Any thoughts on what I could be doing wrong?
PHP:
$progress = 5;
$initialDate = date('m-d-Y');
echo "ini date: " . date('m-01-Y',$initialDate) . "<br>";
$date = date('m-01-Y', strtotime("-$progress months", strtotime(date('m-d-Y',$initialDate))));
echo "date: " . $date . "<br>";
output:
ini date: 12-01-2014
date: 08-01-1969
The 2nd argument to date() must be in internal Timestamp format, i.e. "number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)".
strtotime() is really intended to convert a user input into internal date format. If you want to do math with dates, it is better to use a more precise method. I would suggest reading up on date_add() here: http://php.net/manual/en/datetime.add.php
However, the simplest fix to your code is this:
$progress = 5;
$initialDate = time(); // current timestamp, including seconds
echo "initialDate: $initialDate<br>";
echo "ini date: " . date('m-01-Y',$initialDate) . "<br>";
$date = date('m-d-Y', strtotime("-$progress months", strtotime(date('Y-01-d',$initialDate))));
echo "date: " . $date . "<br>";
Note that I used Y-01-d format for the date fed to strtotime, to go back to the 1st of the month before doing the date math, for two reasons:
Y-m-d format has no danger of being interpreted incorrectly by strtotime, unlike m/d/y and d/m/y.
If you are going back 5 months from Jul 30 to Feb 1, it is safer to have Jul 1 as the intermediate step, not Feb 28. "Safer" meaning you don't even need to test if strtotime handles Feb 28 properly.
You can do this using the DateTime object as well. I find it a little more reliable myself. You basically just initiate a DateTime object and then use its ::sub function to subtract a DateInterval in the amount of $progress months.
$progress = 5;
$initialDate = new DateTime(date("Y-m-01"));
echo "ini date: ".$initialDate->format("m-01-Y")."<br />";
$date = $initialDate->sub(new DateInterval("P".$progress."M"));
echo "date: ".$date->format("m-01-Y")."<br />";
Output:
ini date: 12-01-2014
date: 07-01-2014

Converting Timezones from UTC

I'm having trouble converting timezone's from UTC to a user selected timezone. The problem seems to be Daylight Savings Time.
Here is an example I just coded.
<?php
date_default_timezone_set("UTC");
$timezone = -5.0;
$timestamp = time();
$local_time = $timezone * 3600 + $timestamp;
echo date( "m/d/Y - h:i A", $local_time );
?>
When I run the test file it returns 07/21/2014 - 04:29 PM. The current time is actually 5:29. The problem is Daylight Savings Time, where our clocks are turned back an hour.
How can I remedy this problem, or is there a more effective method for adjusting timestamps?
Easiest solution I see is making users select whether DST is currently in effect where they live, as not every country/timezone uses DST. If it is in effect then simply modifying the $timezone variable to +1 would suffice, but would require each user to manage whether DST is in effect or not.
Thank-you in advance.
EDIT:
I tried using DateTime but it was still off...
<?php
$timestamp = time();
echo 'Unix timestamp: ' . $timestamp . "<br/>";
echo 'Unix date: ' . date( "m/d/Y - h:i A", $timestamp ) . "<br/><br/>";
$dt = date_create_from_format('U', $timestamp);
date_timezone_set($dt, new DateTimeZone('America/New_York'));
$adjusted_timestamp = date_format($dt, 'U') + date_offset_get($dt);
echo 'America/New_York: ' . $adjusted_timestamp . "<br/>";
echo 'America/New_York: ' . date( "m/d/Y - h:i A", $adjusted_timestamp );
?>
The results were off by +2 hours. Returns 7:47 PM, it is currently 5:47 PM.
Unix timestamp: 1405979278
Unix date: 07/21/2014 - 11:47 PM
America/New_York: 1405964878
America/New_York: 07/21/2014 - 07:47 PM

Different results for date() and gmdate()

I found something I can't really explain, maybe someone here can give me a hint.
I have the following test code, that prints 2 formatted timestamps, one for the 31.03.2013 and one for 31.03.2014, using date()and gmdate():
<?php
function print_date($timestamp, $year) {
// Add timezone offset for germany
$timestamp += 3600;
print "in $year\n";
print "date: " . date('d.m.Y H:i:s', $timestamp) . "\n";
print "gmdate: " . gmdate('d.m.Y H:i:s', $timestamp) . "\n";
print "\n";
}
$end_2013 = 1364684400; // 31.03.2013
$end_2014 = 1396216800; // 31.03.2014
print_date($end_2013, '2013');
print_date($end_2014, '2014');
print "Default timezone: " . date_default_timezone_get() . "\n";
The result surprises me:
in 2013
date: 31.03.2013 01:00:00
gmdate: 31.03.2013 00:00:00
in 2014
date: 31.03.2014 01:00:00
gmdate: 30.03.2014 23:00:00
Default timezone: Europe/Berlin
Where does the difference in 2014 come from? My first thought is daylight savings time, but why doesn't that have an effect in 2013?
Why are there 2 hours difference in 2014 but only 1 hour difference in 2013?
Daylight savings for Berlin starts at
2013 Sunday, 31 March, 02:00
2014 Sunday, 30 March, 02:00
Your specified time value for each date is 00:00 on that date, so for 2013 Sunday, 31 March it is before 2am, so no daylight savings; for 2014 it is after 2am on 30th March
Assuming that you have already checked the docs. gmdate and date
change it
print "date: " . date('d.m.Y', $timestamp) . "\n";
print "gmdate: " . gmdate('d.m.Y', $timestamp) . "\n";
with this
print "date: " . date('d.m.Y H:i:s', $timestamp) . "\n";
print "gmdate: " . gmdate('d.m.Y H:i:s', $timestamp) . "\n";
and you will find the difference.
gmdate() always Format in GMT/UTC date and time But date() always format according to the default time zone
Try this
<?php
date_default_timezone_set("Asia/Bangkok");
echo gmdate('Y-m-d H:i:s');
echo date('Y-m-d H:i:s');
?>
Is it a DayLight Saving problem?
According to this , seem 2013-03-31 02:00:00 is changed to 03:00:00

Categories