Is it possible to save the momentjs time to PHP variable and use that variable in PHP, One way is to use ajax to send the current time on some event. So is any other best way for this ?
I would start by passing the variable to your PHP file by whatever means you find practical ($_POST, $_GET, $_COOKIE, etc). Ensure that you use a consistent format for the date
moment(yourDate).format('MM/DD/YYYY').toISOString();
Then use a date library in PHP to parse the date into a new date object you can then manipulate
$js_date = $_REQUEST['youDate'];
$date = DateTime::createFromFormat(DateTime::DATE_ATOM, $js_date);
You should now have a PHP variable containing a DateTime object called $date.
Related
I am trying to create a php session variable which should reference "language":"ENG" (and more specifically ENG) within the table column params.
Example: Originally I've been using a "userLanguage" column and created my php session variable like this:
$_SESSION['userLanguage'] = $result[0]['userLanguage'];
Since "language":"ENG" is only one part of the field's value ({"admin_style":"","admin_language":"","language":"ENG","editor":"","helpsite":"","timezone":""}), this obv. doesn't work anymore. Is there an easy way to pull ENG from language? Help would be much appreciated.
Simply run a json_decode() on the field value.
$params = json_decode($result[0]['params']);
if(isset($params->language)){
$_SESSION['userLanguage'] = $params->language;
}
How to get the cookie's birthday/time in PHP?
The cookie itself contains other data, so was wondering if there is a way to get the exact date and h:m:s when it was created without having to create a second cookie just to hold that information?
I searched but too much interference with the other keywords: set, time, cookie, date that's showing answers, but not to this question.
What you can do is to create an array, serialize that array to a string and store the serialized string inside the cookie:
$mycookieArray = array('cookiedata'=>'some data','creationTime'=>new DateTime());
$mycookiedata = serialize($mycookieArray);
Then, everytime you want to load the correct you have to unserialize the array from the cookiedata:
$mycookieArray = unserialize($mycookiedata);
I am using PHP, MySQL, and javascript. I use php to connect to my database to select appointments. I then echo them in a script tag as arrays of object literals (JSON objects):
appointment[$apptid] = {"time":"8:00", "date":"2012-02-10", "description":"testAppt"};
...
I chose to do it this way over writing an appointment "class" in case I add or remove appointment fields, however I can't figure out for the life of me how to create functions that will apply to this array of objects. Is there anyway to declare these as appointment objects and then write prototype functions without losing the properties?
I'm not 100% sure that I understand what you're after, but if I understand then maybe this is what you want:
First of all, in javascript you better use arrays in a different way than in php, so do something like:
var appointments = [];
appointments.push({"time":"8:00", "date":"2012-02-10", "description":"testAppt"});
Now, once you the array you can do something like:
function doSomething() {
alert(this.time);
}
for (var i = 0; i < appointments.length; i++) {
appointments[i].doSomething = doSomething;
}
Check out JSON.parse: http://www.json.org/js.html
Using this, you can parse your JSON strings into appointment objects that contain your variables as defined in the JSON string, with no prototyped functions.
Then, if you want to get really fancy, you can use the "reviver" parameter to pass in a function in which you could define the functions for each appointment object.
I need to access the PHP internal representation of the $_GET and $_POST arrays inside of my function for a particular page request. Is there a PHP internal representation of these arrays? Like for example $_GLOBAL is a representation of the internal array EG[Symbol_Table]. Otherwise is there any way to identify and access the GET and POST variables inside the symbol table and extract them?
In short I need all the variables that I would get from the $_POST and $_GET arrays but inside the Zend Engine. I am developing an extension that has a function with the format (input parameters, page) where the input parameters are all the variables declared by the php page. Is there any way to access this?
They're PG(http_globals)[TRACK_VARS_GET] and PG(http_globals)[TRACK_VARS_POST] respectively.
In the newer version (I assume after 5.3), you have to use stream "php://input" to access GET or POST data.
You can look into the implementation of file_get_contents function in php to see how to open and read a stream.
I have about 30 variables that I need to pass to 3 functions e.g.
displayform() - where some form data is pulled out from DB and some needs to be entered into the form.
checkform() - which checks if all data is entered properly.
errors() - this will display errors (if any)
processform()- this process all data and store them to DB
Now I am using GLOBAL $variable; to pass those variables between functions, but than I have to declare each variable as global at the function begin and that results in a big file, so I just want to know is there a way to declare variables as globals (preferably only once) so that all functions can use them ?
You can try putting all the variables into an associative array and just passing this array between functions, like:
$omgArray = array();
$omgArray['lolVar1'] = lolVar1;
$omgArray['wowVar3'] = wowVar3;
yeaaaFunction($omgArray);
function yeaaaFunction($omgArray){
echo $omgArray['lolVar1'] . $omgArray['wowVar3'];
}
30 variables? Apart from 30 variables being horrible to maintain, having 30 global variables is even worse. You will go crazy one day...
Use an array and pass the array to the functions as argument:
$vars = array(
'var1' => 'value1',
'var2' => 'value2',
///...
);
displayform($vars);
//etc.
Learn more about arrays.
I have a similar scenario where I wrote a class lib for form handling similar to yours. I store all form data into a single array internally in the form class.
When moving form data outside the class I serialize the array into JSON format. The advantage of the JSON format (over PHP's own serialized format) is that it handles nested arrays very well. You can also convert the character set for all the form fields in one call.
In my application I store all form data as a JSON string in the database. But I guess it all depends on your needs.
You may want to read about Registry pattern, depending on your data, it may be useful or not.