Serialize and Unserialize using base64

I’ve encounter error at offset when using unserialize.

Notice: unserialize(): Error at offset 2 of 52 bytes in file.php on line 8881

So here’s the solution:

If you want to bring multi dimensional array to the next function , you have to serialize it and write to cookie .
This is how you can do it safely without losing offset.

//securely serialize your data
$this->Cookie->write('nama_data_set', base64_encode(serialize($this->data)),false);
 
//how to securely unserialize it on another function
$data_yang_dibaca = unserialize(base64_decode($this->Cookie->read('nama_data_set')));

Seconds to Time – CakePHP Helpers

First, you need to create a new file in app/views/helpers/ and name it saat2masa.php .
Insert this code below & save:

 
<?php
	class Saat2masaHelper extends AppHelper {
 
		function formatMasa($secs) {
		    $times = array(3600, 60, 1);
		    $time = '';
		    $tmp = '';
		    for($i = 0; $i < 3; $i++) {
				$tmp = floor($secs / $times[$i]);
				if($tmp < 1) {
					$tmp = '00';
				} elseif($tmp < 10) {
					$tmp = '0' . $tmp;
				}
				$time .= $tmp;
				if($i < 2) {
					$time .= ':';
				}
				$secs = $secs % $times[$i];
		   }
		   return $time;
		}
	}
?>

Next you need to enable it application wide. Open app_controller.php and add ‘Saat2masa’ to var $helpers array. Example as show below :

var $helpers = array(
  'Html',
  'Form',
  'Javascript',
  'Session',
  'Saat2masa'
);

Lastly, you can call this function in any view file like this ‘$this->Saat2masa->formatMasa(6400)’ which will give you the result in HH:MM:SS format equals to ’01:46:40′ . Example usage in CakePHP 1.3 is :

echo $this->Saat2masa->formatMasa(6400);
 
//or u can use it like this
 
echo $this->Saat2masa->formatMasa($user['time_connected']);

Happy Coding & Weekends guys !
P/s: Lets go to National Youth Day @ Putrajaya [http://www.haribelianegara.org.my/]