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
Related
Here I have set the my Centos time Zone.> sudo hwclock – show
Tue 04 Feb 2014 10:23:10 AM AFT -0.596389 seconds
asia/kabul
Now on PHP I want to echo from 10:23:10 AM to 11:23:10 AM
Here I have set my code for the echoing.
<? echo (date('G', time())+5) ;
echo(':00 To ');
echo (date('G', time())+6);
echo(':00'); ?>
Now as result of my above echoing PHP code I get the result of
15:00 To 16:00
But instead I want to get the echo of below or as what ever my HTTP server time is from NOW to 1 hour next.
10:23:10 AM to 11:23:10 AM
You should try strtotime(). Something like this:
echo date('h:i:s A'), ' to ', date('h:i:s A', strtotime('+1 hour'));
echo date('Y-m-d H:i:s', strtotime('now')), ' to ', date('Y-m-d H:i:s', strtotime('+1 hour'));
For date format, please read: http://www.php.net/manual/en/function.date.php
For your format:
echo date('h:i:s A', strtotime('now')), ' to ', date('h:i:s A', strtotime('+1 hour'));
Add to the time and then print the date.
echo date('G',time()+3600);
time() returns number of seconds since the unix epoch, then you add 3600 seconds for 1 hour and you use that in your date.
You can use explode function.
<?PHP
$time = date('G:i:s');
$eTime = explode(':', $time);
$timePlusOneHour = $eTime+1 . ":{$eTime[1]}:{$eTime[2]}";
echo $time . " To " . $timePlusOneHour;
I am having some trouble trying to convert string to time.
My Code is:
$time = strtotime("14 November, 2013 2:30 AM");
echo $time ."<br />";
echo date("m/d/Y", $time);
I know that strtotime is not magic, and I checked out the acceptable date/time formats but I am not sure how to convert the string to another string without converting it to time first.
What's the easiest way to accomplish this?
Take a look at DateTime::createFromFormat and then call format on the created DateTime instance.
Something like:
$yourTimeString = '14 November, 2013 2:30 AM';
$date = DateTime::createFromFormat('d F, Y h:i A', $yourTimeString);
echo $date->format('m/d/Y');
One way is to rewrite the string using explode and list.
<?php
// here we assume "day month, year time AMPM"
$date = "14 November, 2013 2:30 AM";
// assign a variable to each part of the string
list($day,$month,$year,$time,$ampm) = explode(" ",$date);
// remove the commas at the end of the month
$month = str_replace(',','',$month);
// Now we rewrite the strtotime string
$time = strtotime($month . " " . $day . ", " . $year . " " . $time . " " . $ampm);
echo $time ."<br />";
echo date("m/d/Y", $time);
Php date() function allow natural language string for parsing date,
for Ex:
echo date("d-M-Y", strtotime("first monday of 2019-07")); // returns first monday of july 2019
echo date("d-M-Y", strtotime("last sat of July 2008"));
You can find here php instructions to parsing date as natural languages.
http://php.net/manual/en/datetime.formats.relative.php
I saved a value in MySQL using the bigint column type for a date of August 11, 2013 - 11:55 PM (EST TIME)
This is saved as unix time in the bigint column with value : 1376279700
I understand this to be epoch time since 1970 jan 01,01 00:00:00 in seconds. So I presumed that if I initialize DateTime using any timezone it should yield 08/11/2013 - 11:55 PM ( and whatever timezone used when initialized).
but given following code:
$time1 = new DateTime("#1376279700");
$time1->setTimezone("Europe/London");
echo "Created Europe/London - ".$time1->format(DateTime::RFC822);
and
$time2 = new DateTime("#1376279700");
$time2->setTimezone("America/New_York");
echo "Created America/New_York - ".$time2->format(DateTime::RFC822);
I get these values:
Created: Europe/London - Mon, 12 Aug 13 04:55:00 +0100
and
Created: America/New_York - Sun, 11 Aug 13 23:55:00 -0400
The Europe/London timezone self-adjusts and somehow magically it knows that 1376279700 was created using EST timezone.
I am very confused here. Please shed some light here. I am trying to create a timezone aware function where start date of an event (08/11/2013 11:55 PM) is used by my user's timezone.
The constructor ignores the timezone when you pass a Unix timestamp.
$ts = new DateTime('#946684800');
echo $ts->format('Y-m-d H:i:s') . PHP_EOL; // 2000-01-01 00:00:00
// Setting the time zone does nothing *here* . . .
$pac_tz = new DateTimeZone('America/Los_Angeles');
$ts = new DateTime('#946684800', $pac_tz);
echo $ts->format('Y-m-d H:i:s') . PHP_EOL; // 2000-01-01 00:00:00
// But you can ask for the "same" timestamp
// in a particular time zone.
$ts->setTimeZone($pac_tz);
echo $ts->format('Y-m-d H:i:s') . PHP_EOL; // 1999-12-31 16:00:00
echo '=============' .PHP_EOL;
$time1 = new DateTime("#1376279700");
echo $time1->format('Y-m-d H:i:s').PHP_EOL; // 2013-08-12 03:55:00
$time1_tz = new DateTimeZone("Europe/London");
$time1->setTimezone($time1_tz);
// If it's 2013-08-12 03:55:00 in UTC, what time is it in London?
// London isn't on UTC in the summer (now, as I write this).
echo "Created Europe/London - ".$time1->format(DateTime::RFC822) . PHP_EOL;
$time2 = new DateTime("#1376279700"); // Same as $time1
echo $time2->format('Y-m-d H:i:s').PHP_EOL;
$time2_tz = new DateTimeZone("America/New_York");
// If it's 2013-08-12 03:55:00 in UTC, what time is it in New York?
$time2->setTimezone($time2_tz);
echo "Created America/New_York - ".$time2->format(DateTime::RFC822) . PHP_EOL;
All the output . . .
2000-01-01 00:00:00
2000-01-01 00:00:00
1999-12-31 16:00:00
=============
2013-08-12 03:55:00
Created Europe/London - Mon, 12 Aug 13 04:55:00 +0100
2013-08-12 03:55:00
Created America/New_York - Sun, 11 Aug 13 23:55:00 -0400
I would like convert PST to GMT.
Ex:
PST : 22:00 need to convert in to GMT. I have to consider about DAY LIGHT SAVING TIME month also.
How can i do that?
Use PHP's built-in DateTime class...
Use DateTime objects which have this functionality built in:
$date = new DateTime('2013-08-06 15:00:00', new DateTimeZone('America/Los_Angeles'));
echo "The time in Los Angeles is " . $date->format('Y-m-d H:i:s') . "<br>";
$date->setTimezone(new DateTimeZone('Europe/London'));
echo "The time in London is " . $date->format('Y-m-d H:i:s') . "<br>";
(Example Code) (Full Documentation)
I have to do similar for one my work and here it is how approached this:
//date time in PST
$t = 'Tue Feb 04 23:02:09 PST 2014';
$dateTime = DateTime::createFromFormat("D M d H:i:s \P\S\T Y", $t, (new DateTimeZone('America/Los_Angeles')));
var_dump($dateTime); // date time in PST format
$dateTime->setTimezone(new DateTimeZone('UTC'));
var_dump($dateTime); // date time in GMT format
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.