I have a snippet of code where I want to show the date and time for every time a client uses a discount card. Clients can not have more than two entries per card. However, when I try to display the two entries with the appropriate formatting only the older timestamp formats properly. Code below:
Last Used:
<?php
$timestamp = mysql_to_unix($row->trans_date); //MySql Time stamp 2011-05-31 12:49:59
date_default_timezone_set('America/Chicago'); //Push timestamp ahead 2 hours
$lastuse = date('F j, Y # g:i A', $timestamp); //format date
echo $lastuse;
?>
<?php endforeach; ?>
I have two timestamps coming in 1306871399 and 1306864204. The first stamp successfully processes as May 31, 2011 # 2:49 PM, but the second comes out May 31, 2011 # 12:50 PM.
I am not understanding why only one of the timestamps are being processed. Your feedback is appreciated.
1306871399
- 1306864204
------------
= 7195
7195 seconds = 1hr 59 minutes 55 seconds
May 31/2:49pm -> May31/12:50pm is about 1hr 59 minutes apart
So what's the problem?
You should format your timestamp values for user-display at the MySQL level, instead of using PHP to format them. That way, you don't have to worry about what timezone your PHP server is on (your reference to the Chicago timezone). Try using the MySQL Date Format function to return a MySQL formatted timestamp as a string you can display...
My thinking is that the older timestamp is not being passed through the timezone push, as it is 2 hours behind. Make sure your code is running both values through it.
If that's not it.. has your hosting provider changed recently? Server-time vs. Local-time could be an issue?
Related
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
This is my issue, i have a time stamp coming from php, the servers time is 3 hours off from mine. I have offset the time by three hours but quickly realized that at 2 am the date part of the time stamp reads the day before date (day and month) and that does not change till 3 am. This is an issue because the date is important and i need it to be accurate. I have tried the timezone change but cant seem to get it to work. I live in ohio so thats the time i need and the timezone the server is in is three hours behind. So one of two things can help me, a timezone change that works or offsetting the day by 3h, not only the time. Here is my current code:
$timechange = mktime(date("g")+3, date("i"), 0, date("m"), date("d"), date("y"));
$date = date("D, d M Y g:i",$timechange);
$now = new DateTime('now', new DateTimeZone('America/Ohio')); // whatever your TZ's name happens to be
$now->setTimeZone('America/ServerTZ'); // reset to your server's TZ
$datestr = $now->format('D, d M Y g:i'); // get TZ's time as a nice string
By doing the setTimeZone, you affect the OUTPUT of the function - internally the timestamp is unchanged.
Instead of adjusting the times yourself, try just setting the timezone beforehand.
See http://php.net/manual/en/function.date-default-timezone-set.php
I'm using the unix timestamp to show when a message was posted in my project but when I came to displaying the exact time of the post I realized it was about 12 hours behind.
I'm in the UK and my server is in the US (might be the problem).
Is there a simple way of converting the unix timestamp into a readable British time?
$timestamp = time();
print date("F jS, Y", strtotime($timestamp));
Any help would be great,
Thanks!
At the top of your script, write:
date_default_timezone_set('Europe/London');
Or if your PHP is >= 5.2.0:
date_timezone_set('Europe/London');
Just call date_timezone_set with the appropriate parameter for the UK at the start of your script when displaying the dates (not when recording them; I 'm not sure, but it might result in the "wrong" timestamps being recorded).
Edit: The timezone you want is 'Europe/London'.
try date-default-timezone-set.
date_default_timezone_set('Europe/London');
Use date_default_timezone_set('Europe/London'); to set the time zone to London's time. Not sure if it works with summer/winter time.
the simplies way is to substruct gmt offset. e.g:
echo(date('Y-m-d h:i'), $myvalue - 60 * 60 * $nhours));
where $nhours - time defference in hours.
This one worked for me
date_default_timezone_set('Europe/London');
Ok, I am storing a Date/Time in a MySQL TEXT field. I want to be able to check if the date stored in the database is atleast 48 hours from the current date/time. Here is some code:
<?php
date_default_timezone_set("America/New_York");
$dtformat = "F jS, Y - g:i A";
$datetime = date($dtformat);
?>
The value of $datetime is sent to MySQL and placed in a TEXT column, I would use MySQL DATETIME, but the timezone needs to be different, depending on the users location. So I figured I would make my timestamp in PHP. An example output would be January 11th, 2011 - 6:24 PM
How would I take the current date/time in PHP and find out if my timestamp January 11th, 2011 - 6:24 PM is atleast 48 hours old?
Thanks
First, you should be storing the data and time in the appropriate timestamp field and not a text field.
Moving on...
You could compare the dates like this:
if(strtotime($dateFromDatabase." + 48 hours") <= strtotime("now")) {
//It's at least 48 hours old, do your stuff
}
<?php
date_default_timezone_set("America/New_York");
$dtformat = "F jS, Y - g:i A";
$datetime = date($dtformat);
$i_datetime = strtotime($datetime);
$i_48_hours = strtotime('now -48 hours');
if ($i_datetime < $i_48_hours)
{
echo "The date $datetime is older than 48 hours old.";
}
?>
I would use MySQL DATETIME, but the timezone needs to be different, depending on the users location"
so use mysql timestamp date type, it is designed for such special purposes.
http://dev.mysql.com/doc/refman/5.1/en/timestamp.html
I don't think it is any reason to continue following current terrible solution. Better rewrite your code for using timestamp.
I've got a very strange bug cropping up in some PHP code I've got. The page is managing student enrolments in courses. On the page is a table of the student's courses, and each row has a number of dates: when they enrolled, when they completed, when they passed the assessment and when they picked up their certificate.
The table data is generated by PHP (drawing the data from the DB), and Javascript actually renders the table. The output from PHP is JS code which looks something like this:
var e = new Enrolment();
e.contactId = 5801;
e.enrolId = 14834;
e.courseId = 3;
e.dateEnrolled = new Date(1219672800000);
e.dateCompleted = new Date(-1000); // magic value meaning they haven't completed.
e.resultDate = new Date(1223647200000);
e.certDate = new Date(1223560800000);
e.result = 95;
e.passed = true;
enrolments[14834] = e;
In the database, all the date fields are stored as DATE (not DATETIME) fields.
The bug is that the dates are being displayed as one day off. I would suspect that this has a lot to do with the server being in an area which has daylight saving, whereas here there isn't any (meaning the server time is one hour off). This explains a lot, especially how the data preparation and rendering is being done in two different timezones. That is: the server is saying to the client that the person completed at midnight on the 15th August, and the client is interpreting that as 11pm on the 14th and therefore displaying 14th August.
But here's the confusing part: it's only doing that for the resultDate and certDate fields! I've copied the data to my local server and have found that the production server is actually sending a different timestamp (one which is off by 1 hour) just for those two fields, whereas the dateEnrolled field is the same.
Here's the output using the exact same code and data from the database:
// local server (timezone GMT+1000)
e.dateEnrolled = new Date(1219672800000); // 26 Aug 2008 00:00 +10:00
e.dateCompleted = new Date(-1000);
e.resultDate = new Date(1223647200000); // 11 Oct 2008 00:00 +10:00
e.certDate = new Date(1223560800000); // 10 Oct 2008 00:00 +10:00
// production server (timezone GMT+1100)
e.dateEnrolled = new Date(1219672800000); // 26 Aug 2008 00:00 +10:00
e.dateCompleted = new Date(-1000);
e.resultDate = new Date(1223643600000); // 10 Oct 2008 23:00 +10:00 **
e.certDate = new Date(1223557200000); // 09 Oct 2008 23:00 +10:00 **
I can understand if this was a problem with Daylight Saving not being accounted for, but notice how the dateEnrolled is the same?
The PHP code which converts the MySQL date to a unix timestamp is this:
list ($year, $month, $day) = explode ('-', $mysqlDT);
$timestamp = mktime (0,0,0, $month, $day, $year);
Any ideas about how to fix this?
Thats because you use mktime which is locale specific. That is it will convert it to the number of seconds from 00:00:00 1970-1-1 GMT, and that is offset by 1 hour with one timezone.
You should also remember that the javascript does use the same timezone as the browser, not the web page.
e.resultDate = new Date(year, month - 1, day);
This will make sure the date is the same for every viewer from every timezone.
Or you can use gmmktime and use the UTC methods in Date.
Ok, I just figured out why it's mucking up one date but not the other. Daylight savings wasn't in effect in August. facepalm
always store dates/datetimes in GMT/UTC
take a good look at the query that retrieves these values, anything different about the ones being adjusted?
if not, are they all timestamp or date or datetime?
It is mosly likely to be day light saving issue.
The reason why it doing it only for resultDate and certDate is that dateEnrolled is in August, daylight saving normally begins/ends in late September or early October.
Set the date.timezone ini setting to your app's timezone, using apache.conf, .htaccess or ini_set().