I've got a JSON variable ($epoch) which contains the date in epoch (1370777177), and I'd like to convert it to the following format and output it:
date "+%F %R" -ud #1370777177
which returns:
2013-06-09 11:26
So I figure using php to echo the command is one way to do it. Is there a way to convert the epoch to the format above and append it to a DIV using JS? That might be a lot easier. I've tried creating a PHP variable which encodes the JSON var:
$date = json_encode($epoch);
echo exec ('date "+%F %R" -ud #%date');
But that doesn't work.
echo json_encode($epoch)
returns 'null'. The JSON variable is in a external .js file.
Is there a way to convert the epoch to the format above and append it to a DIV using JS?
Yes, there is.
Also exec() will scale awesomely.
Related
I got this format from the wp_postmate table. How can I convert it to a readable format?
a:11:{i:2;a:3:{s:10:"field_name";s:7:"Address";s:11:"field_value";s:47:" govindpuri kalkaji, new delhi-110019";s:11:"field_label";N;}i:3;a:3:{s:10:"field_name";s:4:"City";s:11:"field_value";s:9:"NEW DELHI";s:11:"field_label";N;}i:6;a:3:{s:10:"field_name";s:14:"DateOfPurchase";s:11:"field_value";s:10:"2020-09-15";s:11:"field_label";N;}i:5;a:3:{s:10:"field_name";s:5:"Email";s:11:"field_value";s:31:"sharikbillionaire#gmail.com";s:11:"field_label";N;}i:1;a:3:{s:10:"field_name";s:9:"Last-name";s:11:"field_value";s:5:"matin";s:11:"field_label";N;}i:8;a:3:{s:10:"field_name";s:16:"Nameoftheproduct";s:11:"field_value";s:10:"Mohd Afzal";s:11:"field_label";N;}i:7;a:3:{s:10:"field_name";s:7:"OrderId";s:11:"field_value";s:10:"1236547896";s:11:"field_label";N;}i:4;a:3:{s:10:"field_name";s:7:"PinCode";s:11:"field_value";s:6:"110019";s:11:"field_label";N;}i:10;a:3:{s:10:"field_name";s:14:"mc4wp_checkbox";s:11:"field_value";s:2:"No";s:11:"field_label";N;}i:9;a:3:{s:10:"field_name";s:10:"reviewfile";s:11:"field_value";s:32:"e570b19fc4a826c333e4ac78ee19393c";s:11:"field_label";N;}i:0;a:3:{s:10:"field_name";s:9:"your-name";s:11:"field_value";s:4:"mohd";s:11:"field_label";N;}}
The above data was stored in a serialized format so you can simply use unserialize() to get the data in an array.
I have a text link to pass a date GET in format Y-m-d because is comming from MySQL like that.
The page receiving this GET uses the follow code to echo the date I got from previous page
the link is like this
page.php?thedate=2015-06-30
The page I need to show this date must convert this date like this
<?php echo date('d-m-Y',strtotime($_GET["thedate"]));?>
so we show 30-06-2015
But it show with this code
31-12-1969
What can the problem be?
Your code is looking OK. But few thoughts, what could go wrong:
At first: Try the following code:
<?php
var_dump($_GET['thedate']); // check what it is?
date_default_timezone_set('Europe/Warsaw'); // set default timezone to your zone / user zone
echo date('d-m-Y',strtotime('2015-06-30')); // check that date is working with plain text
?>
At first remember to always use date_default_timezone_set('Europe/Warsaw');. It can be really important for Your application. Second, enter the date in plain text mode. If this works, it mean that the date function is working ok, and there is something wrong with Your GET or after GET variable manipulation.
Second: NEVER, EVER DON'T VALIDATE _GET VARIABLES. Sorry for double negation, but maybe becouse of that, it can be more memorable ;).
Always parse the data that user enters in the URL. Always parse:
_GET data
_POST data
_REQUEST data
_SERVER data (yes, some of them can be manipulated
all other vulnerable data...
So summing up: Your code (the one You entered is OK), check the GET variable and the after get variable manipulation. And always check all the data that is not trusted.
More on validating data:
filter_var: http://php.net/manual/en/function.filter-var.php
preg_match: http://php.net/manual/en/function.preg-match.php
htmlspecialchar: http://php.net/manual/en/function.htmlspecialchars.php
According to one of the comments solution with using urldecode on _GET variable:
<?php echo date('d-m-Y',strtotime(urldecode($_GET["thedate"])));?>
"Warning! The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results."
Source: http://php.net/manual/en/function.urldecode.php
I send a QueryString formatted text like bellow to a php Script via Ajax:
title=hello&custLength=200&custWidth=300
And I want to convert this text to a JSON Object by this result in PHP:
{
"title" : "hello",
"custLength" : 200,
"custWidth" : 300
}
How can i do that. Does anyone have a solution?
Edit :
In fact i have three element in a form by title , custLength and custWidth names and i tried to send these elements via serialize() jquery method as one parameter to PHP script.
this code is for Send data to php:
customizingOptions = $('#title,#custLength,#custWidth').serialize();
$.post('cardOperations',{action:'add','p_id':p_id,'quantity':quantity,'customizingOptions':customizingOptions},function(data){
if (data.success){
goBackBtn('show');
updateTopCard('new');
}
},'json');
in PHP script i used json_encode() for convert only customizingOptions parameter to a json.
But the result was not what I expected and result was a simple Text like this:
"title=hello&custLength=200&custWidth=300"
I realize this is old, but I've found the most concise and effective solution to be the following (assuming you can't just encode the $_GET global):
parse_str('title=hello&custLength=200&custWidth=300', $parsed);
echo json_encode($parsed);
Should work for any PHP version >= 5.2.0 (when json_encode() was introduced).
$check = "title=hello&custLength=200&custWidth=300";
$keywords = preg_split("/[\s,=,&]+/", $check);
$arr=array();
for($i=0;$i<sizeof($keywords);$i++)
{
$arr[$keywords[$i]] = $keywords[++$i];
}
$obj =(object)$arr;
echo json_encode($obj);
Try This code You Get Your Desired Result
The easiest way how to achiev JSON object from $_GET string is really simple:
json_encode($_GET)
this will produce the following json output:
{"title":"hello","custLength":"200","custWidth":"300"}
Or you can use some parse function as first (for example - save all variables into array) and then you can send the parsed output into json_encode() function.
Without specifying detailed requirements, there are many solutions.
I need to push some JSON data to my website which I would like to read in PHP. What type of file should I make this? A PHP file with the JSON inside of a variable? I understand how to make a text file with JSON encoded data in it, but how do I get this into PHP? Should I use a PHP include with the JSON-encoded data in it assigned to a variable? Or should I read the file from PHP and put the contents into a variable?
Save your json string as plain text, then you can use:
$file = yourfile
$data = file_get_contents($file);
$parsed = json_decode($data);
// compacted:
$parsed = json_decode(file_get_contents($file));
See file_get_contents() and json_decode().
The advantage of doing this (versus storing it in a PHP file then including it) is that now any program or language that understands JSON can read the file.
The question is too vague for a definite "do this" answer, but here are some options and what they might be most suitable for:
Turn the json data into a PHP data structure. If this is a one-time thing (meaning you won't be getting a new json file every day or week or hour), then reading a file (file_get_contents) and parsing JSON (json_decode) for every request is a pretty big waste of resources since that data isn't changing on a regular basis. Just turn JSON key/value objects into PHP associative arrays, JSON strings into PHP strings, etc.
Just serve the json file. If this is data that will just wind up going to the client to be used in javascript anyway, there's no need to do anything special with it on the server, just parse the json on the client.
Put it in a database. This may be a little heavy-handed, but if you really need it in PHP and not just the client, and it is going to be changing or growing on a regular basis, it may be worth it to have something that handles this use case appropriately.
In PHP, I'm trying to read an Excel file using COM():
$rrc_app = new COM("Excel.application");
$rrc_workbook = $rrc_app->Workbooks->Open($filename);
$rrc_worksheet = $rrc_workbook->Worksheets('Top sheet');
$rrc_worksheet->activate;
I tried to extract the value of a cell containing a "Time" value:
$review_time = $rrc_worksheet->Range("C30")->value;
However, it returns a decimal number:
0.604166666667
I know I can use the Format() function of VB, but I don't know from what object in PHP to call it from. The following:
$review_time = Format($rrc_worksheet->Range("C30")->value, "hh:mm:ss");
Gives:
Fatal error: Call to undefined function Format() in C:\xampplite\htdocs\pmc\index.php on line 40
Do you happen to know how I can call this Format() function using PHP?
Thanks in advance
Format is a function of the VBA.String module, so it's not part of the Excel COM library and I'm not sure if it is accessible via COM at all.
However, you can use the Text property instead of Value: This returns a formatted string (according to the cell format in Excel) rather than the underlying value:
$review_time = $rrc_worksheet->Range("C30")->Text;
EDIT: If the cell does not have the correct format yet, you can change the format before reading the Text property (untested):
$rrc_worksheet->Range("C30")->Select();
$rrc_app->Selection->NumberFormat = "hh:mm:ss";
$review_time = $rrc_worksheet->Range("C30")->Text;
Format() looks like a Visual Basic function. It is not necessarily available to you throught the Excel COM object you are communicating with - only if that object declares it somewhere. The VB function range is not imported automatically into PHP when talking to a COM object.
Can you try $rrc_app->Format()?
If nothing works, I recommend figuring out what the time value stands for (I'm sure you can find out here on SO) and converting it in PHP.