$eve['start']['dateTime'] = 2013-05-02T14:00:00+05:30;
$current_date = date("m-d-Y",strtotime($eve['start']['dateTime']));
$start_time = date("H:i A",strtotime($eve['start']['dateTime']));
when i use the above code am getting it as 05-02-2013 08:30 AM
But i should get 05-02-2013 2:00 PM
why this time difference and shows wrong any idea?
The time difference is not wrong. You are getting the correct date and time for a timezone at +0:00. To fix this, set your timezone.
your formatters seems to be incorrect, to get the desired output use the code below. H is used for 24 hour format with leading zero. h is used for 12 hour format.
date("h:i A");
$eve['start']['dateTime'] = "2013-05-02T14:00:00+05:30"; // Missing quotes in your code?
$current_date = date("m-d-Y",strtotime($eve['start']['dateTime']));
Should return 05-02-2013 08:30 AM because your server has timezone sat to GMT+0. If you take 14:00 and subtracts with 5 and a half hour (from +0530 to +0000), it should be 08:30.
To avoid this, you have to set default timezone on your server or in your script.
Related
I've set the timezone on my website pages as such:
date_default_timezone_set("America/Los Angeles");
I've declared a time variable as such:
$time = date("h:i:sa");
then adding it to the database. I then retrieve the time from the database like such, trying to reformat it:
$time2 = date("h:i A", strtotime($row['Time']));
and then printing out the time:
echo $time2;
This method, however, messes up AM and PM. If $time was added to the database at 11:59:00 at night, for example, it will print out 11:59 AM, not 11:59 PM. I think the issue is that when the time is added to the database, no clear distinction is made between AM and PM. However, I'm still unsure how to fix this. I've tried adding the time with the AM PM already in it, like such:
$time = date("h:i:sa A");
and then accessing it like such:
$time2 = $row['Time'];
but this still doesn't fix the bug. Is there a way around this while keeping my 12 hour format? Or do I have to change to 24 hour format to get this to work?
Please start storing dates/times in Y-m-d/H:i:s format in DATETIME, DATE or TIME fields. Your life, and ours, would be much easier.
Until then, use this PHP solution:
$dt = DateTime::createFromFormat('!h:i:sa', $row['Time']);
echo $dt->format('H:i:s');
demo
or mysql solution:
SELECT STR_TO_DATE('08:46:07am','%h:%i:%s%p')
I know it's gonna sound you strange.. but it's happening..
I am trying mktime() function to create a seconds string:
$time = mktime(21,0,0,3,29,2014);
echo date("d-M, h:i A", $time);
And then I add 21600 (6 Hours) seconds in it..
$newstr = $time+21600;
echo echo date("d-M, h:i A", $newstr);
I Expect this output:
29-Mar, 09:03 PM
30-Mar, 03:03 AM
But I am getting this:
29-Mar, 09:03 PM
30-Mar, 04:03 AM // It must be 03:03 AM
Any one knows what the problem is..? I am using xampp.
Your timezone is set to a region for which Daylight Savings Time is enacted on March 30, 2014, so the latter date ends up being adjusted to DST and is one hour later than you'd expect mathematically.
http://www.timeanddate.com/news/time/europe-starts-dst-2014.html
You can verify this by printing your before and after dates with the timezone marker e and DST marker I included in date's mask.
Maybe you can try to add this line before your code
ini_set('date.timezone','UTC');
I am storing the time of day as a number of seconds since midnight. I have a number that should be 8:00 am:
//3600 secs / hour * 8 = 28800
$time = 28800;
var_dump(date('h:i a', $time ));
My output is:
string(8) "01:00 am"
Based on my location, I am -7:00 GMT, so I can see where I would get 1:00 am, but how do I do format this time to show 8:00 am, essentially making it ignore the current GMT setting while formatting this time?
two ways.
first you may try gmdate() function which output the raw GMT time .
and the other way you can set timezone before you use date function.
as follow .
date_default_timezone_set('Asia/Shanghai');
echo date('H:i:m', time());
I figured this out. The solution is to use gmdate(). It will format a raw timestamp to GMT.
I am writing a function to convert an internal date format into a timestamp value. However, when I print the date out in YYYY-MM-DD HH format the date is 12 hours off.
The code below gives me the wrong date and time. I am expecting 2011-03-25 13 but instead I am getting 2011-03-25 01.
date_default_timezone_set("Europe/London");
$epoch = mktime(13,0,0,3,25,2011);
echo date('Y-m-d h', $epoch);
When I use the following code I expect 2001-02-01 01 and get what I expected.
date_default_timezone_set("Europe/London");
$epoch = mktime(1,0,0,2,1,2011);
echo date('Y-m-d h', $epoch);
It seems that the 12 hour offset starts on March 25th in the 13th hour.
Any idea why this happens and how do I keep it from happening? Does this have to do with the different day light savings dates? The server timezone is set to "America/Los_Angeles".
It works, you're just using the wrong format code, take H (24 hour format) instead of h (12 hour format):
date_default_timezone_set("Europe/London");
$epoch = mktime(13,0,0,3,25,2011);
echo date('Y-m-d H', $epoch);
Read the PHP Manual, it explains each code in detail.
This is my current code that doesn't seem to work correctly.
echo date("h:i", 1*60*60) ; /* 1*60*60 mean 1 hours right */
The result is 03:00 when it should be 01:00.
Where is this going wrong?
i want to do this
1- employee come in time admin select it
2- employee come - leave saved in mysql as timestamp
Now I'm trying to make admin see how many hours user late
mean
user date time default late
user1 11-09-2011 09:10 09:00 10 min
user1 12-09-2011 08:00 09:00 -60 min
If you output just date("Y-m-d H:i:s",0) you should see that it's not 1970-01-01 00:00:00 as it should be. It's because date is affected by your local timezone. For example I'm in GMT +3, and date("Y-m-d H:i:s") gives me 1970-01-01 03:00:00.
So the answer is you are not in GMT timezone (probably in GMT+2), and date is affected by it.
UPDATE
The following code outputs 1970-01-01 00:00:00, so it's definitely time zones.
date_default_timezone_set('UTC');
echo date("Y-m-d H:i:s", 0);
Hovewer, I can't see any mention about it in PHP's date manual.
The problem is due to your timezone (looks like GMT+2).
Date calculations in PHP are based on the configured server timezone. For example mine shows
$ php -r 'echo date("h:i", 3600), PHP_EOL;'
11:00
The second argument in date() function must be UNIX timestamp, seconds passed from Jan 1 1970. You need to make time according to that value.
You have probably not setup time zones, which should produce a PHP warning or notice if omitted.
It occurs to me that what SamarLover think he wants is
gmdate("h:i", 1 * 60 * 60);
Err, if I'm right, date()'s second param is a unix timestamp, so the seconds since 1970.
You have to get time() and add 60*60 to it.
echo date("h:i", time()+60*60); // get current timestamp, add one hour