Monday, October 10, 2016

Useful PHP Time functions

Create date string which adds 30 days from current date
strtotime('today' + '90 days')
date("Y-m-d", strtotime("+30 days")


Create DateTime object from date string
new_expiry_date = strtotime(date("Y-m-d", strtotime("+30 days")));

Convert DateTime to MongoDate
new MongoDate($new_expiry_date);

 //Adding days to mongodate  
 function add_days_mongodate($mongo_date, $days)  
 {  
   $expiry_date = strtotime(date("d-m-Y H:i:s", $mongo_date->sec));  
   $new_expiry_date = $expiry_date + ($days * 24 * 60 * 60); //Add 30 Days  
   return $new_expiry_date;  
 }  
 function convert_string_to_mongodate($date)  
 {  
   $carbon_date = Carbon::createFromFormat('d/m/Y', $date);  
   $mongo_date = new MongoDate(strtotime($carbon_date->toDateString()));  
   return $mongo_date;  
 }  
 // Calculate date from two datetime  
 function days_between($str_date1, $str_date2)  
 {  
   //Get 1 day in milliseconds  
   $one_day = 1000 * 60 * 60 * 24;  
   $date1 = strtotime($str_date1);  
   $date2 = strtotime($str_date2);  
   $difference = $date1 - $date2;  
   $days = $difference/$one_day;  
   return $days;  
 }  
 // Calculate hours from two datetime  
 function hours_between($str_date1, $str_date2)  
 {  
   $date1 = strtotime($str_date1);  
   $date2 = strtotime($str_date2);  
 //  Log::info($date1);  
 //  Log::info($date2);  
   $difference = $date1 - $date2;  
   $hours = $difference/3600;  
   Log::info($hours);  
   return $hours;  
 }  
 //Format mongo date to displayable date string  
 function parse_mongo_date($mongo_date)  
 {  
 //  $mongo_date == null  
   if(is_a($mongo_date, 'MongoDate'))  
     $new_date =date('d-m-Y H:i:s',$mongo_date->sec);  
   else  
     $new_date = $mongo_date;  
   return $new_date;  
 }  
 function convert_to_mongo_date($date)  
 {  
   return new MongoDate(strtotime(date(DATE_ISO8601, $date->sec)));  
 //  new MongoDate(strtotime(date(DATE_ISO8601, $mongo_date->sec) - "30 days"));  
 }  
 function get_mongo_date_from_string($str_date)  
 {  
   $mongo_date = new MongoDate();  
   if(isset($str_date)) {  
     $carbon_date = Carbon::createFromFormat('d/m/Y', $str_date);  
     $mongo_date = new MongoDate(strtotime($carbon_date->toDateString()));  
   }  
   return $mongo_date;  
 //  new MongoDate(strtotime(date(DATE_ISO8601, $mongo_date->sec) - "30 days"));  
 }  

Sunday, October 9, 2016

Backing up database to amazon s3

Go to the following address to download s3tools for linux
http://s3tools.org/usage

apt-get install unzip to install zip for Linux if the downloaded folder needs to unzip

apt-get install s3cmd to install s3tools from command line

Configure the s3
s3cmd --configure to configure the tool for the first time

Access Key: xxxxxxxxxxxxxxxx
Secret Key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Encryption password is used to protect your files from reading
by unauthorized persons while in transfer to S3
Encryption password: your_password
Path to GPG program [/usr/bin/gpg]:

When using secure HTTPS protocol all communication with Amazon S3
servers is protected from 3rd party eavesdropping. This method is
slower than plain HTTP and can't be used if you're behind a proxy
Use HTTPS protocol [No]: NO

On some networks all internet access must go through a HTTP proxy.
Try setting it here if you can't conect to S3 directly
HTTP Proxy server name:

New settings:
  Access Key: AKIAJWMP76CEGCZYJJJA
  Secret Key: oxDlDDsXMgYQ+W7ZvmdPKEsjKXoAfZ1DF2J7vK6k
  Encryption password: whitecat
  Path to GPG program: /usr/bin/gpg
  Use HTTPS protocol: False
  HTTP Proxy server name:
  HTTP Proxy server port: 0

Test access with supplied credentials? [Y/n] Y
Please wait, attempting to list all buckets...
Success. Your access key and secret key worked fine :-)

Now verifying that encryption works...
Success. Encryption and decryption worked fine :-)

Save settings? [y/N] y

Configuration saved to '/root/.s3cfg'

Now you can upload the file to s3 using the following commands

s3cmd put /your/path/server/$FILE_NAME.tar.gz s3://backups.joydash.com/path/$FILE_NAME.tar.gz