I have written this Javascript to convert a timestamp to something readable. It works perfectly. However I need to do it with PHP but don't know how. Obviously I don't want an alert of the time, but I'd like to have it as a PHP variable. Any ideas?
<script>
var bmsTime ="39845.03";
var date = new Date('31 dec 1899');
date.setTime(date.getTime() + bmsTime* 24 * 60 * 60 *1000);
alert (date);
</script>
Use the date function, it takes an additional parameter called timestamp. But in php timestamp is number of seconds, not milliseconds as in javascript, so divide it by 1 000:
echo date('l jS \of F Y h:i:s A', $javascript_timestamp / 1000);
The short answer: You cannot properly do this, as you do not know the timezone the browser/client is using.
Long answer (using the server timezone - or whatever is configured for PHP):
$bmsTime = 39845.03;
$date = mktime(0, 0, 0, 12, 31, 1899);
$date += $bmsTime * 24 * 60 * 60; // $date are the seconds relative to "the epoc" (1970-01-01 UTC)
echo date('c', $date);
Related
When I call strtotime("2016-05-06 15:00:00 +15.98 hours") I'd expect 2016-05-07 06:58:48 but instead I get 2016-05-10 02:00:00. What gives?
You can test here yourself:
Use strtotime: http://php.fnlist.com/date_time/strtotime
Convert output int to timestamp: http://www.epochconverter.com/
Floats aren't supported in date formatting in PHP
It seems that you can't do addition in strtotime that way. In addition, a bug for decimal point has been reported here
What you can do is add the time in 2 separate variable just like William's answer
Try this instead:
//60 * 60 * 15.98 = 57,528 seconds
$add = round(60 * 60 * 15.98);
$timestamp = strtotime("2016-05-06 15:00:00") + $add;
$dt = date("Y-m-d H:i:s", $timestamp);
echo $dt; //2016-05-07 06:58:48
This will calculate to 2016-05-07 06:58:48
As for why it incorrectly added the 15.98 hours is more complex. There has been a reported bug for this problem by PHP, though currently floats aren't supported in date formatting in PHP. Since you can not directly use floats in date formatting, you must substitute something like "1.5 years" with "18 months", or do arithmetic before and round it like this:
//60 * 60 * 15.98 = 57,528 seconds
$timeToAdd = round(60 * 60 * 15.98);
And then call strtotime() like in the above example
$date = strtotime("2016-05-06 15:00:00") + $timeToAdd;
I know this question has been asked many times as I have found a few on google and also on stackoverflow.
but none of them explained how to format my datetime in my php so it works in combination with jquery countdown timer. so I am going to ask it here in a hope i get someone shed a light on this for me.
Basically what i am trying to do is to create a countdown timer which will work with mysql datetime.
the datetime is stored in mysql so All need to do is to get the correct format in my php so the countdown timer could work with it.
I am using this plugin: http://keith-wood.name/countdown.html
and here is what i have so far:
PHP formatting:
$end_date = date("m d Y H:i:s T", strtotime($row["end_date"]));
Jquery/Javascript code:
<script type="text/javascript">
$(function(){
var countdown = $('#countdown'),
ts = new Date(<?php echo $end_date * 1000; ?>),
finished = true;
if((new Date()) > ts)
{
finished = false;
}
$('#defaultCountdown').countdown({
timestamp : ts,
callback : function(days, hours, minutes, seconds)
{
var message = "";
message += days + " days, ";
message += hours + " hours, ";
message += minutes + " minutes, ";
message += seconds + " seconds ";
message = (finished ? "Countdown finished" : "left untill the New Year");
countdown.html(message);
}
});
});
</script>
when i run this code, all i get is 0 hours, 0 minutes, 0 seconds.
I can only suspect that the issue is from formatting the datetime in my php section!
or am i missing something else as well?
okay I have managed to minify the code to this:
<script type="text/javascript">
$(document).ready(function () {
$('#defaultCountdown').countdown({
until: new Date(<?php echo $end_date; ?>),
compact: true
});
});
</script>
and changed the php to this:
$end_date = date("Y, n, j, G, i, s", strtotime($row["end_date"]));
However, the time shown in the coutdown timer is wrong (way off).
the $end_date is: September 22 2013 23:30:00 GMT in mysql datetime
but the jquery countdown timer is showing:
34d 06:21:48
2013, 9, 22, 23, 30, 00
34days and 6 hours blah blah is absolutely wrong!
what am i doing wrong here now?
The JavaScript Date object is constructed as follows:
Date(year, month, day, hours, minutes, seconds, milliseconds)
That means you probably should be doing something along these lines:
$end_date = date("Y, n, j, G, i, s", strtotime($row["end_date"]));
Sources:
JavaScript Date-object
PHP date-function
EDIT:
In addition, I seem to have found the problem in the jQuery Countdown manual:
A note on Date - the JavaScript Date constructor expects the year,
month, and day as parameters. However, the month ranges from 0 to 11.
To make explicit what date is intended (does a month of 3 mean March
or April?) I specify the month from 1 to 12 and manually subtract the
1. Thus the following denotes 25 December, 2010.
So, you'd have to split the string, substract 1 from the month and rebuild...
$tmp_date = explode(', ', $end_date);
$tmp_date[1] = $tmp_date[1] - 1;
$end_date = implode(', ', $tmp_date);
Link to jsFiddle
I'm using a PHP script to grab data from Active Directory using LDAP..
When I get the user values for 'lastlogon' I get a number like 129937382382715990
I've tried to figure out how to get the date/time from this but have no idea, can anybody help?
Read this comment on the PHP: LDAP Functions page.
All of them are using "Interval" date/time format with a value that represents the number of 100-nanosecond intervals since January 1, 1601 (UTC, and a value of 0 or 0x7FFFFFFFFFFFFFFF, 9223372036854775807, indicates that the account never expires): https://msdn.microsoft.com/en-us/library/ms675098(v=vs.85).aspx
So if you need to translate it from/to UNIX timestamp you can easily calculate the difference with:
<?php
$datetime1 = new DateTime('1601-01-01');
$datetime2 = new DateTime('1970-01-01');
$interval = $datetime1->diff($datetime2);
echo ($interval->days * 24 * 60 * 60) . " seconds\n";
?>
The difference between both dates is 11644473600 seconds. Don't rely on floating point calculations nor other numbers that probably were calculated badly (including time zone or something similar).
Now you can convert from LDAP field:
<?php
$lastlogon = $info[$i]['lastlogon'][0];
// divide by 10.000.000 to get seconds from 100-nanosecond intervals
$winInterval = round($lastlogon / 10000000);
// substract seconds from 1601-01-01 -> 1970-01-01
$unixTimestamp = ($winInterval - 11644473600);
// show date/time in local time zone
echo date("Y-m-d H:i:s", $unixTimestamp) ."\n";
?>
This is the number 100-nanosecond ticks since 1 January 1601 00:00:00 UT.
System time article in Wikipedia can give you more details.
What about this:
$timeStamp = 129937382382715990;
echo date('Y-m-d H:i:s', $timeStamp);
EDIT ------
I just tried the following and noticed that this method wont work unless the clock on your machine is set 10 years in the future. Below is the code I used to prove the above pretty much useless unless you do more processing maybe..
$time = time();
echo date('Y-m-d H:i:s', $time);
echo "<br />";
$timeStamp = 129937382382715990;
echo date('Y-m-d H:i:s', $timeStamp);
In my case I'm using Pentaho. With a Modified Javascript value you can convert the values, lastLogon is the column I wanna convert from data stream:
calendar = java.util.Calendar.getInstance();
calendar.setTime(new Date("1/1/1601"));
base_1601_time = calendar.getTimeInMillis();
calendar.setTime(new Date("1/1/1970"));
base_1970_time = calendar.getTimeInMillis();
ms_offset = base_1970_time - base_1601_time;
calendar.setTimeInMillis( lastLogon / 10000 - ms_offset); //lastLogon is a column from stream
var converted_AD_time = calendar.getTime(); // now just add this variable 'converted_AD_time' to the 'Fields' as a show in the image below
I'm currently reporting file modified time like so:
$this->newScanData[$key]["modified"] = filemtime($path."/".$file);
$modifiedtime = date($date_format." ".$time_format, $this->newScanData[$key]["modified"]);
To me I thought there was nothing wrong with that but a user of my code is reporting the time being 4 hours out. The only reason why I can think of this is because the server is in a different timezone to the user. Each user has a variable I can use $gmt_offset that stores the time zone that user is in. $gmt_offset is stored as a basic float offset.
The server could be in any timezone, not necessarily in GMT-0. The server might not be in the same timezone as the user.
How do I get $modifiedtime to have the correct time for the user in his timezone based on $gmt_offset?
filemtime() will return a unix timestamp based on the server's clock. Since you have user to gmt offset available, you must convert the unix timestamp to GMT and then into user's timszone as follows:
<?php
list($temp_hh, $temp_mm) = explode(':', date('P'));
$gmt_offset_server = $temp_hh + $temp_mm / 60;
$gmt_offset_user = -7.0;
$timestamp = filemtime(__FILE__);
echo sprintf('
Time based on server time.........: %s
Time converted to GMT.............: %s
Time converted to user timezone...: %s
Auto calculated server timezone...: %s
',
date('Y-m-d h:i:s A', $timestamp),
date('Y-m-d h:i:s A', $timestamp - $gmt_offset_server * 3600),
date('Y-m-d h:i:s A', $timestamp - $gmt_offset_server * 3600 + $gmt_offset_user * 3600),
$gmt_offset_server
);
// Output based on server timezone = PKT (+05:00 GMT) and user timezone = PDT (-07:00 GMT)
// Time based on server time.........: 2011-06-09 03:54:38 PM
// Time converted to GMT.............: 2011-06-09 10:54:38 AM
// Time converted to user timezone...: 2011-06-09 03:54:38 AM
// Auto calculated server timezone...: 5
What you need is the strtotime() function. Changed date to gmdate, converting your servers time to GMT
For example if you need the time format like 10:00:00
gmdate("H:i:s", strtotime($gmt_offset . " hours"));
More info here:
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.gmdate.php
$modifiedtime = date($date_format." ".$time_format, $this->newScanData[$key]["modified"] + ($gmt_offset * 3600));
$gmt_offset should be of type float, not int -- some time zones can have fractional difference, like GMT +09:30 for Adelaide
Hi I'm using php and sql through odbc to write a program and i hav got abit stuck in a part where i want to display the current date/time in the format date('Y-m-d H:i:s) but it only displays the gmt time. I want to add 8hours to it.Can any of you b able to help me.Thank you so much
Check out date_default_timezone_set. You can do something like:
date_default_timezone_set('America/Los_Angeles');
print 'Current datetime is: ' . date('Y-m-d H:i:s');
You could use that to set the timezone to whatever timezone you need time to be at, and then use date normally. Alternatively, you can do this, using strtotime:
print 'Current datetime is: ' date('Y-m-d H:i:s', strtotime('+8 hours'));
If you're looking for a way to display a timestamp in a user's local time, you can use JavaScript:
function showtime(t)
{
if (t == 0)
{
document.write("never");
return;
}
var currentTime = new Date(t);
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
document.write();
if (minutes < 10){
minutes = "0" + minutes;
}
if (seconds < 10){
seconds = "0" + seconds;
}
document.write(month + "/" + day + "/" + year + " " +
hours + ":" + minutes + ":" + seconds + " ");
if(hours > 11){
document.write("PM");
} else {
document.write("AM");
}
}
Then if you need to display a time, just make a call to it in the HTML and splice in the value from PHP:
<script type="text/javascript">showtime(<?=$time."000"?>)</script>
I would steer clear of the timezone method.
If i understood correctly, you want to add time, thus change it. An example could be, A task has been created NOW, and must be complete in 8 hours. The timezone method would only change the display of the date and time. Only change the timezone setting if you know your visitor's timezone, and datetime's must be shown relative to them.
Now: 1234418228 is 2009/02/12 00:57:08 in Montreal or 2009/02/11 09:57:08 in San Francisco. It's the exact same moment.
Appending to the first answer, date() and strtotime() are your friends.
strtotime( "+8 hours", $now )
$now being a timestamp of when it's supposed to relate to. So if your start time isn't time(), you can still use that. eg
strtotime( "+8 hours", strtotime( "2009/03/01 00:00:00" ); (8AM on 2009/03/01)
However, when dealing with intervals counted in weeks, or less, i prefer doing it 'mathematically'
$StartTime = strtotime( "2009/03/01 13:00:00" );
$EndTime = $StartTime + ( 8 * 60 * 60 );
date( "Y/m/d H:i:s", $EndTime ) ==> "2009/03/01 21:00:00"
3600 seconds in an hour, 86400 in a day.
You can't use this method for months, quarters or years because the number of seconds they last varies from one to the next.
If you want to use time for a certain timezone, then using date_default_timezone_set() is preferred. anyway you can provide the date() function another parmater: int timestamp. an integer representing the timestamp you would like date() to return the information about.
so if you would like to show date('Y-m-d H:i:s') for now you can use this:
$now = date('Y-m-d H:i:s', time() ); // time() returns current timestamp.
// if you omit the second parameter of date(), it will use current timestamp
// by default.
$_8hoursLater = date('Y-m-d H:i:s', time()+60*60*8 );
$_8hoursBefore = date('Y-m-d H:i:s', time()-60*60*8 );