Showing posts with label PHP Code. Show all posts
Showing posts with label PHP Code. Show all posts

Tuesday 4 August 2015

Get Random Values From Array IN PHP

Get Random Values From Array IN PHP



I am sharing my example code to get random values from PHP array. May this small code can be helpful for someone.





<?php

// array
$arr_subjects = array('Science', 'Maths', 'GK', 'Law', 'History', 'Arts', 'Chemistry',
    'Computers', 'Bio', 'Music');
// random number
$c = rand(1, 6);
//shuffle array elements
shuffle($arr_subjects);

//print random values in an array
print_r(array_slice($arr_subjects, $c, $c));
// prints $c'th elements from $c'th position in an array

Sunday 14 June 2015

Parse YouTube Time Code PHP

Parse YouTube Time Code PHP





I am sharing my PHP code to parse YouTube time.


<?php

function covtime($youtube_time)
{
    $start = new DateTime('@0'); // Unix epoch
    $start->add(new DateInterval($youtube_time));
    return $start->format('g:i:s');
}
echo date("H:i:s");
echo "<br>";
echo covtime('PT2H34M25S'); // => 2:34:25
echo "<br>";
echo date("H:i:s");
echo "<br>";
preg_match_all('/(\d+)/', "PT2H34M25S", $parts);
echo "<br>";
echo date("H:i:s");
echo "<pre>";
print_r($parts);

?>

Sunday 30 November 2014

Mailchimp Integration CodeIgniter


Codeigniter MailChimp API v2 Wrapper

I am sharing my Codeigniter Code regarding Mailchimp integration. May this small code can be helpful for someone.

Reference - https://github.com/benbowler/codeigniter-mailchimp-api-v2



<?php 

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *         http://example.com/index.php/welcome
     *    - or - 
     *         http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */

    public function __construct() {
        parent::__construct();
        $this->load->library('Mailchimp_library');
    }

    public function index()
    {       
        $list_id = '';
        $lists = $this->mailchimp_library->call('lists/list');
        if(isset($lists["data"][0])) {
            $list_id = $lists["data"][0]["id"];
        }
        if(!empty($list_id)) {
            $result = $this->mailchimp_library->call('lists/subscribe', array(
                'id'                => $list_id,
                'email'             => array('email'=>'sourabhgupta3838@gmail.com'),
                'merge_vars'        => array('FNAME'=>'Sourabh', 'LNAME'=>'Gupta'),
                'double_optin'      => false,
                'update_existing'   => true,
                'replace_interests' => false,
                'send_welcome'      => false,
            ));
            print_r($result);
        }
        $this->load->view('welcome_message');
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

Create .ICS file using PHP code

Recently worked on creating a .ics file in PHP after a very long time, code so thought to share with everybody. Please find below the comple...