I'm not very experienced in ajax and having trouble formatting my date from a MySQL database.
I've tried using the php date() function but can't seem to get it to work properly. What would be the best way to format the date in this?
$(document).ready(function(){
$.ajax({
type: "GET",
url: "../api/presentation/read.php",
dataType: 'json',
success: function(data) {
var response="";
for(var user in data){
response += "<tr>"+
"<td>"+data[user].date+"</td>"+
"<td>"+data[user].title+"</td>"+
"<td>"+data[user].speaker+"</td>"+
"<td><a href='update.php?id="+data[user].id+"'>Edit</a> | <a href='#' onClick=Remove('"+data[user].id+"')>Remove</a> | <a href='#' onClick=Active('"+data[user].id+"')>Active</a></td>"+
"</tr>";
}
$(response).appendTo($("#presentations"));
}
});
});
I have tried something along the lines of
"<td><?php echo date("+data[user].date+",dd-mm); ?></td>"+
This gives an output of "+01am31am[00000000UTCThu, 01 Jan 1970 00:00:00 +0000].01am31UTC+" which I suspect is because no value is being given to the date function?
I'd like the output to be in the format of dd-mm HH-MM instead of the MySQL datetime format of "2019-10-29 10:00:00"
You are mixing PHP and JavaScript in a wrong way.
First, you MUST know that PHP is executed on server side, BEFORE any HTML or JavaScript exists on your browser.
That said, lets read your attempt again:
""
Here, the PHP part of script will be run on server side, and the data[...] Variable don't exists there, also it's inside a string (between double quotes). Since this string is not a valid date, it will output the start of unix epoch.
To solve this, you need to separate your concerns here.
First way
Your php script called by ajax should return the date in some valid date format. I suggest using ISO8601 format since JavaScript parses this format automatically when using Date object.
Use JavaScript to format that date in the desired output format.
Second way:
Directly convert the date on PHP using the date function, but: you do it entirely on the PHP script. Don't mix both languages. They are totally separated and runs in different moment's and have isolated scopes.
Make the change in your Select query and then do as you have now.
SELECT DATE_FORMAT(NOW(), '%d.%m.%Y %H:%i:%s')
Result
DATE_FORMAT(NOW(), '%d.%m.%Y %H:%i:%s')
27.10.2019 20:40:41
instead of the function now() put in your columnname
Related
The main issue: I gather the initial parameter in the datetime format, and use php date function to convert it to a readable format. From this format, I need to convert the date back to the same datetime parameter using javascript.
I have tried the getTime() function in javascript to get a datetime parameter conversion.
<?php
$_REQUEST['date'] = 1500786000;
$date=date('Y-m-d', $_REQUEST['date']);
echo "<span id='date'>$date</span>";
?>
<script>
var pdate=new Date('#date').val().replace(/-/,'\/'));
console.log(pdate.getTime());
</script>
The original datetime parameter starts as 1500786000, is formatted as 2017-02-23 via PHP.
The javascript conversion is 1500786000000, three 0s off from the expected format.
I would prefer not to use an alternative library, so anything that can be used in native javascript would be most helpful.
If you follow the same steps this is as expected. You just need to divide the value by 1000 to get the value you are expecting.
The original datetime parameter starts as 1500786000 because it's in seconds.
In JavaScript it will return the number of milliseconds since 1970/01/01.
var seconds = new Date().getTime() / 1000; // if you want it to be seconds
Why don't you set both values in that span and use that as below:
<?php
$_REQUEST['date']=1500786000;
$date=date('Y-m-d', $_REQUEST['date']);
echo "<span id='date' data-date='".$_REQUEST['date']."'>$date</span>";
?>
<script>
var visibleDate= $('#date').val(); //You actual visible date
var rawDate= $('#date').data('date'); //You raw date
</script>
You don't need to make conversion in JS again. Hope it helps you.
I have a JSON output from a non-configurable system where various date/time variables have values like \/Date(1422691756316)\/
How can I get that into a readable format (I need to display it via PHP and Javascripts).
You could deserialize the JSON output via json_decode() and then you could try to parse the string to an timestamp with strtotime (http://php.net/manual/de/function.strtotime.php), then you could create a date from thins timestamp using the date() method (http://php.net/manual/de/function.date.php) with a given formatting string an the timestamp.
But I'm not sure if your date string is ready to be parsed. In all cases you may to remove the \/Date( and )\/ parts of the string.
EDIT: Looks like your string is not a timestamp, strtotime fails. You may have to check what kind of timestamp/ datestamp/ whatever your string is.
I am trying to parse a date string i get from php through ajax call(which is irrelevant for now) using new Date().
however i keep getting wrong results.
My string is 2013-05-09 20:56:17
When i do
var something = new Date("2013-05-09 20:56:17");
alert(something.getMonth());
It keeps alerting 0
In my opinion for some reason new date cant parse this string.
Is there a way to specify the date format for new Date() in JS ?
My current solution is to import php's: date() and strtotime() and use them :
alert(date('m', strtotime("2013-05-09 20:56:17")));
This works however I have to use external js lib and I am pretty sure there is a better JS way to achieve that.
If you use slashes instead of hyphens, it works:
var something = new Date("2013/05/09 20:56:17");
alert(something.getMonth());
It's easy enough to replace any hyphens in a string with slashes first if you need to (say, if you were getting the date string from somewhere else):
var something = new Date("2013-05-09 20:56:17");
something = something.replace('-', '/');
It seems JavaScript's Date constructor doesn't recognize date formats with hyphens, or at least not that particular format.
Choose a different format specifier in PHP for your ajax dates. The format you expect and the format expected by the javascript are different.
var something = new Date("2013-05-09T20:56:17");
Note the 'T' which appears as a literal separator and marks the beginning of time per ISO 8601
Reference for various [browser] javascript date formats
W3 DateTime
Microsoft IE DateTime
Mozilla [Firefox] DateTime
Google DateJs
And lastly, the PHP date format specifier list:
PHP Date
PHP DateTime
Note the 'DATE_ISO8601'; but I suggest not using that at this time. Instead use 'DATE_ATOM' which may produce a date format more widely supported (comments suggest it makes iPhones happier and no issues with other browsers).
To use it in PHP:
$something = new DateTime('NOW');
echo $something->format('c');
echo $something->format(DateTime::ATOM);
I'm returning some json from one of my controllers similar to the following:
render :json => {
:date => "new Date(new Date('Jan 01 2000').getTime() + #{500.seconds.to_i} * 1000)"
}
I'm expecting it to return a new date set to 500 seconds past midnight (or exactly 00:08:20). This json gets passed and processed by a JavaScript function that's expecting a date, however it seems to be interpreting it as a string.. I get an error message similar to:
Type mismatch. Value new Date(new Date('Jan 01 2000').getTime() + 500
* 1000) does not match type date
Running that in the Firebug console produces a perfectly valid date, however. How can I get JavaScript to inerpet my json as a date?
Edit
To provide a little more context I'm sending a series of JSON requests to a JavaScript function that build charts using Google's Visualization API.
JSON is not Javascript, it is just a data serialization format, much like XML.
What you have is actually a string of Javascript code that you need to interpret (or avoid having to interpret, preferably). The only way for that to be valid JSON is, indeed, for it to be a string value. Even then, it should not parse correctly without being inside an array or an object property.
You should refactor whatever's returning that Javascript code to instead return just 'Jan 01 2000', and then do that processing in Ruby. Or however it would be best refactored; it's hard to tell without more context.
i made this code for inserting data to an system mail that know to work with xml file.
the problem is that i try to create some javascript code for getting the current date of day, and then put it inside the filed date, but without success.
i know hot create the date in javascript, my problem is in thx xml file, i mean
how can i impplemt the date inside the filed date in the xml file.
the code(xml side):
123456
NOW
COMPLETE
ENGLISH
1274
liran
**
413
3280
86308
;
UNIX
email;dateofday
liroy7#gmail.com;(i want here to return the date from the javascript)
thanks,
I'm not sure I understand, but you can use getTime() on your Date objec to insert it into the XML file as milliseconds, and if you need to convert it back into a JavaScript object you can parse it directly.
function putInXmlFile(d) {
writeToXML(d.getTime());
}
function getFromXmlFile() {
var date = new Date(getFromXML());
}
It works because the JavaScript Date object can parse from the milliseconds from the epoch (which is returned by getTime).