Reputation: 2301
Is there an equivalent of the JavaScript code below possible in PHP?
I want to echo out the amount with PHP so that the amount is still visible on the page whenever JavaScript has been disabled by the visitor.
I'm not that good with PHP so I would appreciate it if anyone could help me out or point me to the right directions.
JavaScript
var start = new Date(2014, 2, 1);
var rate = 1 / (30 * 1000);
var amount = Math.floor((new Date() - start) * rate);
Upvotes: 0
Views: 369
Reputation: 15882
Phew... It looked easier than it was!
Ok, it was really tricky, because you must take into account JS client Offset. If you copy and paste next code into a foo.php
and you execute it with an active console, you'll see both (PHP and JS) in console are the same.
You must be careful with the value of the client OFFSET (in my case, UTC+1, 60 minutes), so I corrected in my PHP. You should send the OFFSET of the client you use and use it to correct it into the server, or better, say that the server time is whatever the time zone you're and the data are related to that timezone (due to you want it to use this with clients with no JS available, so you won't get the client Offset)
<script>
// Creates a JavaScript Date instance that represents a single moment in time.
// Date objects are based on a time value that is the number of milliseconds since 1 January, 1970 UTC.
// new Date(year, month [, day, hour, minute, second, millisecond]);
// REALLY TRICKY!! Month starts in 0!
var start = new Date(2014, 2, 1);
console.log( 'OFFSET (minutes): ' + start.getTimezoneOffset() );
console.log('Date Start: ' + start.getTime() );
console.log('Date NOW: ' + new Date().getTime() );
var rate = 1 / (30 * 1000);
console.log('Date substract: ' + (new Date() - start ) );
var amount = Math.floor((new Date() - start) * rate);
console.log('Amount: ' + amount);
</script>
<?php
// Returns the Unix timestamp corresponding to the arguments given.
// This timestamp is a long integer containing the number of seconds between the
// Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
// int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )
// As month starts in 0 in js, here is month 3
$start = mktime(0, 0, 0, 3, 1, 2014);
echo 'Date Start MkTime: ' . $start . '<br>';
echo 'Date Start MkTime + JS OFFSET ( -60 min * 60 sec): ' . ( $start - 3600 ) . '<br>';
$start = $start - 3600;
echo 'Date Start: ' . $start . '<br>';
echo 'Date NOW: ' . time() . '<br>';
// You don't need divide between 1000 because PHP is seconds, in JS you get microsecs
$rate = 1 / 30;
echo 'Date substract: ' . ( time() - $start ) . '<br>';
$amount = floor( ( time() - $start) * $rate );
echo 'Amount: ' . $amount . '<br>';
Here you have what I get from my browser:
Upvotes: 2
Reputation: 5981
<?php
$start = strtotime("2011-02-01");
$rate = 1 / (30 * 1000);
$amount = floor((time() - $start) * $rate);
echo $amount;
?>
Upvotes: 1
Reputation: 2141
Try this:
$start = mktime(0, 0, 0, 2014, 2, 1);
$rate = 1 / (30 * 1000);
$amount = ((time() - $start) * $rate) | 0;
Upvotes: 2