Saturday, 10 September 2016

How to change the date format of other timezone which is not the current timezone?

Change the default timezone to the required timezone.
Then, convert the date format by using strtotime() and date() functions.
And at last, change the default timezone to the earlier one.


$defaultTimeZone = date_default_timezone_get(); // Taking default timezone
date_default_timezone_set('EST'); // Setting EST as default timezone
echo date('m/j/Y G:i',strtotime('2011-10-05 16:35:49.917'))."<br/>";
date_default_timezone_set('UTC');

File upload code


<form action="" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<p>
<label for="file">Path name:</label>
<input type="input" name="loc" id="loc" value="sites/default/files/"/>

<p>

<input type="submit" name="submit" value="Submit" />
</form>

<?php
if (isset($_POST['loc'])){
if ( $_FILES["file"]["error"] > 0)
{
 echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
 move_uploaded_file($_FILES["file"]["tmp_name"] , $_POST["loc"].$_FILES["file"]["name"]);
 echo "Stored at: " . $_POST["loc"].$_FILES["file"]["name"] ;
}
}
?>

Is blank screen coming for the urls mentioned in routes.yml file of sapphire framework?

One error might be the folder naming convention, the folder should be named as _config, here, the characters should be in small. I wasted almost a day because of this issue(i mentioned _Config). So, thought of might be helpful for others.

How to interact with mssql server through php?

To interact with SQL server in WINDOWS, its based on php version, need to place either mssql.dll or sqlsrv dll's.  This is stated clearly in http://in1.php.net/manual/en/mssql.requirements.php . In UNIX / LINUX, need to install the FreeTDS library and use mssql_connect.
Now, use sqlsrv_connect/mssql_connect to connect to server, sqlsrv_query / mssql_query to run a query over the server, sqlsrv_fetch_array / mssql_fetch_array to fetch the data if needed.
Examples:
$server = "xxx.xx.xx.xxx";
$database = "testDb";
$UID = "user";
$PWD = "password";
$sql_query = "EXEC test.Get_Details";
/* For sqlsrv: */
if(function_exists('sqlsrv_connect')){
$connection = sqlsrv_connect($server,array( "Database"=>$database,"UID"=>$UID,"PWD"=>$PWD));
$msqldbobj = sqlsrv_query( $connection, $sql_query);
while( $row = sqlsrv_fetch_array( $msqldbobj, SQLSRV_FETCH_ASSOC) ) {
            print_r($row);
       }
}
/* For mssql: */
else if(function_exists('mssql_connect')){
$connection = mssql_connect($server,$UID,$PWD);
mssql_select_db($database, $connection);
$msqldbobj = mssql_query($sql_query);
while( $row = mssql_fetch_array($msqldbobj, MSSQL_ASSOC) ) {
            print_r($row);
       }
}else{
echo 'server drivers not available';
}

How to parse XML Youtube Feeds in PHP into array format?

        $ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "gdata.youtube.com/feeds/api/playlists/PL802BE7047AA4DE0E?max-results=50&amp;orderby=published&amp;v=2");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$xml = simplexml_load_string($output);
$youtubedata = json_decode(json_encode((array)$xml), TRUE);

How to get next month starting date based on a date in PHP?

$d = new DateTime(date( 'Y-m-d' ));
$d->modify('first day of next month');
echo $d->format('Y-m-d');

Wednesday, 13 July 2016

How to load jQuery UI for anonymous users in Drupal 7?

jQuery UI is no longer loads for anonymous users. We can load it in the preprocess html function as follows:
function template_preprocess_html(&$vars){
        drupal_add_library('system', 'ui');
}