How To Synchronize Your PHP and MySQL Timezones

time zones msql php 300x172 How To Synchronize Your PHP and MySQL Timezones

PHP and MySQL are web processes that operate with their own separate default configurations for timezones. It is unlikely that you will have timezone issues during development or when you use a single server. PHP and MySQL can use the server’s time configurations in these situations.

Many developers set PHP as the default configuration if:

One’s application can be set to the user’s timezone.
If different timezone requirements exist in two or more running applications
One cannot set the default timezones because the application was installed on shared server.

Developers also change from dates to Unix timestamp integers for simpler usability but there is a risk of running into variety of issues such as additional processing of translating dates for your application, the difficulty to see the dates in a table, the difficulty of acquiring date-based SQL queries as well as the inability to use MySQL date/time functions like NOW().

However it is possible to synchronize the timetables of the PHP and MySQL. A single configuration variable is required to define your PHP timezone.

1.<?php
2. define(‘TIMEZONE’, ‘America/New_York’);

The code below will establish the default timezone for PHP:

date_default_timezone_set(TIMEZONE);

For MySQL , use this code:

SET time_zone=’offset’;

Offset is the difference of time in relation to the GMT zone. For instance, if you want to set the timezone a hour ahead of GMT you would replace offset with ‘+1:00′. For the code to work, you need to have a +/- sign with the hours even at 0:00.

To synchronize PHP and MySQL together, you must first start with the following PHP code:

1. $now = new DateTime(); 
2. $mins = $now->getOffset() / 60;

From there you can convert the number of seconds in the offset to minutes with the following calculation:

1. $sgn = ($mins < 0 ? -1 : 1); 
2. $mins = abs($mins); 
3. $hrs = floor($mins / 60);
4. $mins -= $hrs * 60;

From here, format the string with this function:

$offset = sprintf(‘%+d:%02d’, $hrs*$sgn, $mins);

After this step, you can now use the SET time_zone command. Now your PHP and MySQL codes should be synchronized.The following code is an example:

$db = new PDO(‘mysql:host=localhost;dbname=test’, ‘dbuser’, ‘dbpassword’); 
$db->exec(“SET time_zone=’$offset’;”);

Quoted from: http://blog.awardspace.com/how-to-synchronize-your-php-and-mysql-timezones

Advertisements

Simple String EnCrypt + DeCrypt function

Sometimes you need some simple way to avoid stuff is read by someone else.
Some texts you want to keep private.
Can be if you save information in files, at a server with limited options for file protection.

If your need of security is not that high,
this script makes it a bit more difficult to get to your information.
Most people would never be able to decode your text.
If you have stuff that needs high security, you should use other tools.
Sure is possible to add more complicated operations,
but I wanted to keep it small, fast and simple.

This little function will use a Secret Key to encrypt text.
And you would have to use same Key to decrypt it back.
There is only one function for both encrypt/decrypt.
And you call it the same way always.

Script below will output:

Code:
Key: mysecretkey
To be or not to be, that is the question
Yv3gf2jf+kvy9gj#p`8+qqlm3lp2q|n%hx|`qj}k
To be or not to be, that is the question

I think it is easy to see how to use this function.
Here is my code:

PHP Code:

<?php
// String EnCrypt + DeCrypt function
// Author: halojoy, July 2006
function convert($str,$ky=''){
if(
$ky=='')return $str;
$ky=str_replace(chr(32),'',$ky);
if(
strlen($ky)<8)exit('key error');
$kl=strlen($ky)<32?strlen($ky):32;
$k=array();for($i=0;$i<$kl;$i++){
$k[$i]=ord($ky{$i})&0x1F;}
$j=0;for($i=0;$i<strlen($str);$i++){
$e=ord($str{$i});
$str{$i}=$e&0xE0?chr($e^$k[$j]):chr($e);
$j++;$j=$j==$kl?0:$j;}
return
$str;
}
///////////////////////////////////

// Secret key to encrypt/decrypt with
$key=‘mysecretkey’; // 8-32 characters without spaces

// String to encrypt
$string1=‘To be or not to be, that is the question’;

// EnCrypt string
$string2=convert($string1,$key);

// DeCrypt back
$string3=convert($string2,$key);

// Test output
echo ‘<span style=”font-family:Courier”>’.“\n”;

Quoted from: halojoy @ phpbuilder.com

PHP: Encrypt and Decrypt

<?PHP
/*
Description : A function with a very simple but powerful xor method to encrypt
and/or decrypt a string with an unknown key. Implicitly the key is
defined by the string itself in a character by character way.
There are 4 items to compose the unknown key for the character
in the algorithm
1.- The ascii code of every character of the string itself
2.- The position in the string of the character to encrypt
3.- The length of the string that include the character
4.- Any special formula added by the programmer to the algorithm
to calculate the key to use
*/
FUNCTION ENCRYPT_DECRYPT($Str_Message) {
//Function : encrypt/decrypt a string message v.1.0  without a known key
//Author   : Aitor Solozabal Merino (spain)
//Email    : aitor-3@euskalnet.net
//Date     : 01-04-2005
$Len_Str_Message=STRLEN($Str_Message);
$Str_Encrypted_Message=””;
FOR ($Position = 0;$Position<$Len_Str_Message;$Position++){
// long code of the function to explain the algoritm
//this function can be tailored by the programmer modifyng the formula
//to calculate the key to use for every character in the string.
$Key_To_Use = (($Len_Str_Message+$Position)+1); // (+5 or *3 or ^2)
//after that we need a module division because can´t be greater than 255
$Key_To_Use = (255+$Key_To_Use) % 255;
$Byte_To_Be_Encrypted = SUBSTR($Str_Message, $Position, 1);
$Ascii_Num_Byte_To_Encrypt = ORD($Byte_To_Be_Encrypted);
$Xored_Byte = $Ascii_Num_Byte_To_Encrypt ^ $Key_To_Use;  //xor operation
$Encrypted_Byte = CHR($Xored_Byte);
$Str_Encrypted_Message .= $Encrypted_Byte;//short code of  the function once explained
//$str_encrypted_message .= chr((ord(substr($str_message, $position, 1))) ^ ((255+(($len_str_message+$position)+1)) % 255));
}
RETURN $Str_Encrypted_Message;
} //end function
?>

sample use of the function

$Str_Test=”This function is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation in any version
of the License.”.”<br>”.”This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.”.”<br>”.”Hello Aitor, Wellcome Home”.”<br>”;
ECHO $Str_Test.”<br>”;
$Str_Test2 = ENCRYPT_DECRYPT($Str_Test);
ECHO $Str_Test2.”<br><br>”;
$Str_Test3 = ENCRYPT_DECRYPT($Str_Test2);
ECHO “<br>”.$Str_Test3.”<br>”;
?>

Quoted from: weberdev.com