Why don't PHP and Javascript's timestamps match?
JavaScript uses milliseconds as a timestamp, whereas PHP uses seconds. As a result, you get very different dates, as it is off by a factor 1000.
So remove three zeroes at the PHP side:
echo date('Y-m-d H:i:s', 1313690400);
Using PHP to Match a Javascript Timestamp
If you don't need (millisecond) presision, then just divide & Math.floor the javascript's function. So:
time();
and
Math.floor((new Date).getTime()/1000)
should return the same value at the same time.
unable to get date and time from timestamp in php?
Looks like you've got milliseconds in that timestamp. Try this:
$timestamp = 1483625713000 / 1000;
$date = date('d-m-Y', $timestamp);
$time = date('Gi.s', $timestamp);
var_dump($date);
var_dump($time);
Outputs
string(10) "05-01-2017"
string(7) "1415.13"
What is the difference between client-side and server-side programming?
Your code is split into two entirely separate parts, the server side and the client side.
|
---------->
HTTP request
|
+--------------+ | +--------------+
| | | | |
| browser | | | web server |
| (JavaScript) | | | (PHP etc.) |
| | | | |
+--------------+ | +--------------+
|
client side | server side
|
<----------
HTML, CSS, JavaScript
|
The two sides communicate via HTTP requests and responses. PHP is executed on the server and outputs some HTML and maybe JavaScript code which is sent as response to the client where the HTML is interpreted and the JavaScript is executed. Once PHP has finished outputting the response, the script ends and nothing will happen on the server until a new HTTP request comes in.
The example code executes like this:
<script type="text/javascript">
var foo = 'bar';
<?php
file_put_contents('foo.txt', ' + foo + ');
?>
var baz = <?php echo 42; ?>;
alert(baz);
</script>
Step 1, PHP executes all code between <?php ?>
tags. The result is this:
<script type="text/javascript">
var foo = 'bar';
var baz = 42;
alert(baz);
</script>
The file_put_contents
call did not result in anything, it just wrote " + foo + " into a file. The <?php echo 42; ?>
call resulted in the output "42", which is now in the spot where that code used to be.
This resulting HTML/JavaScript code is now sent to the client, where it gets evaluated. The alert
call works, while the foo
variable is not used anywhere.
All PHP code is executed on the server before the client even starts executing any of the JavaScript. There's no PHP code left in the response that JavaScript could interact with.
To call some PHP code, the client will have to send a new HTTP request to the server. This can happen using one of three possible methods:
- A link, which causes the browser to load a new page.
- A form submission, which submits data to the server and loads a new page.
- An AJAX request, which is a Javascript technique to make a regular HTTP request to the server (like 1. and 2. will), but without leaving the current page.
Here's a question outlining these method in greater detail
You can also use JavaScript to make the browser open a new page using window.location
or submit a form, emulating possibilities 1. and 2.
JavaScript to PHP timestamp failing, adds about a month and a half to the time
That's because in Javascript's Date
objects the month range is 0-11, not 1-12. That's for the "month" part. Try this:
var mn = j('#time_month :selected').val() - 1; //month
Or better, generate the <option>
s with values ranging from 0 to 11.
Plus, this statement:
hr = j('#time_hour :selected').val()+12;
Remember that j('#time_hour :selected').val()
is a string, not a number, so you have to convert it before adding 12, or else you'd get something like "0412"
. You're creating a date with the hour count set to 412, i.e. 17 days and 4 hours. And 17 days are addes to the day count.
Fix it like this:
hr = +j('#time_hour :selected').val() + 12;
I used the plus unary operator.
(P.S.: there's a lot of room to improve your code, starting with readability and jQuery object caching. Take some time improving your Javascript coding skills before getting used to some bad practices.)
Related Topics
Select Statement with Fetch_Array in MySQLi Prepared Statements
How to Add Custom Fonts in Tcpdf
How to Determine If a PHP File Is Loaded via Cron/Command Line
Strpos() with Multiple Needles
Easiest Way to Implode() a Two-Dimensional Array
Date Function to Display All Dates Between Two Dates
Woocommerce: Display Also Product Variation Description on Cart Items
Php: Check If Xml Node Exists with Attribute
Can Mongodb and Its Drivers Preserve the Ordering of Document Elements
Utf-8 Characters in Preg_Match_All (Php)
Is It Acceptable to Use a Mix of Object Oriented Style with Procedural Style in Coding PHP
Set JavaScript Variable from PHP
Switch of PHP Versions Not Working on MAC
Wordpress, Nginx Proxy and Subdirectory: Wp-Login.PHP Redirects to Domain