Skip to content

Instantly share code, notes, and snippets.

View OkoyaUsman's full-sized avatar
🏠
Available for freelance job

Okoya Usman OkoyaUsman

🏠
Available for freelance job
View GitHub Profile
@OkoyaUsman
OkoyaUsman / detect-mobile-redirect.txt
Created January 4, 2018 12:34
How to detect mobile device and transfer visitor to mobile version of your website in PHP, JavaScript, Apache, jQuery, nginX,
This is quick tutorial about How to detect mobile device and transfer visitor to mobile version of your website.
If you don't have a mobile responsive website and you created separate mobile version of website.
If user open your website in mobile then he/she should automatically redirect mobile version of your website.
Then the following function will help you to achieve this goal.
Like you have seen in many popular website like facebook.com.
If you open facebook on mobile's browser you will be automatically redirected on their mobile version m.facebook.com.
// Here is the function in PHP
<?php
$useragent=$_SERVER['HTTP_USER_AGENT'];
@OkoyaUsman
OkoyaUsman / country-ip-redirect.txt
Created January 4, 2018 12:46
Redirect domain according to country IP address using php or .htaccess
Suppose you have three sub domain like http://au.example.com, http://ng.example.com, http://in.example.com Australia, Nigeria, India.
And If some one type http://example.com then he/she redirected county specific url as per geo ip location. Here is the solution.
// By PHP
First of all download geoplugin library and call it on your page.
http://www.geoplugin.com/_media/webservices/geoplugin.class.phps
<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
@OkoyaUsman
OkoyaUsman / phpunzip.php
Created January 4, 2018 12:52
Unzip a ZIP file using php
<?php
/*
Here I have created a quick function in php to extract zip file.
Unzipping file is now very easy using php.
In php there is a function called ZipArchive::extractTo, using this predefined function you can easily extract zip archive.
*/
function unzip($file, $location) {
$zip = new ZipArchive;
if ($zip->open($file) === TRUE) {
@OkoyaUsman
OkoyaUsman / AgeCalcFromDOB.php
Created January 4, 2018 13:00
Calculate Age from Date of Birth in PHP
<?php
function getAge($dob) {
$today = date("Y-m-d");
$diff = date_diff(date_create($dob), date_create($today));
return $diff->format('%yYears, %mMonths, %dDays');
}
// You can add the above function in your php library and use any where to calculate the age of the user by their date of birth.
echo getAge('19-10-1988');
// Output: 27Years, 10Months, 19Days
?>
@OkoyaUsman
OkoyaUsman / timerange.php
Created January 4, 2018 13:06
Create time range function in php
<?php
// Below is a php time range function you can add in your php library and call it where you want to add time range dropdown.
function createTimeRange($startTime, $endTime, $interval = '15 mins', $format = '12') {
try {
if(!isset($startTime) || !isset($endTime)) {
throw new exception("Start or End time is missing");
}
$startTime = strtotime($startTime);
$endTime = strtotime($endTime);
$currentTime = time();
@OkoyaUsman
OkoyaUsman / cc-validate.php
Created January 4, 2018 13:21
Credit card validation script in PHP
<?php
function CCValidate($type, $cNum) {
switch ($type) {
case "American":
$pattern = "/^([34|37]{2})([0-9]{13})$/";//American Express
return (preg_match($pattern,$cNum)) ? true : false;
break;
case "Dinners":
$pattern = "/^([30|36|38]{2})([0-9]{12})$/";//Diner's Club
return (preg_match($pattern,$cNum)) ? true : false;
@OkoyaUsman
OkoyaUsman / php-regex-meta-description.php
Created January 5, 2018 10:08
How to PHP regex match an HTML document's meta description
<?php
function parseDescription($html) {
// Get the 'content' attribute value in a <meta name="description" ... />
$matches = array();
// Search for <meta name="description" content="Buy my stuff" />
preg_match('/<meta.*?name=("|\')description("|\').*?content=("|\')(.*?)("|\')/i', $html, $matches);
if (count($matches) > 4) {
return trim($matches[4]);
}
@OkoyaUsman
OkoyaUsman / advanced-mediaqueries.css
Created January 16, 2018 14:35
Advanced CSS Media Queries for All The Common Devices
/* ------ Large screens ----------- */
@media only screen and (min-width : 1824px) {
/* Styles go here.. */
}
/* ------ Desktops and laptops ----------- */
@media only screen and (min-width : 1224px) {
/* Styles go here.. */
}
/* ------ Smartphones (portrait and landscape) ----- */
@OkoyaUsman
OkoyaUsman / nocopyjs.html
Created January 28, 2018 08:47
Disable Right Click In JavaScript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Github</title>
</head>
<body>
<script language="javascript">
var message="";
function clickIE() {if (document.all) {(message);return false;}}
@OkoyaUsman
OkoyaUsman / seconds-to-time.php
Created February 3, 2018 11:42
PHP Function to Convert Seconds into Years, Months, Days, Hours, Minutes and Seconds.
<?php
function convertSecToTime($sec){
$date1 = new DateTime("@0"); //starting seconds
$date2 = new DateTime("@$sec"); // ending seconds
$interval = date_diff($date1, $date2); //the time difference
return $interval->format('%y Years, %m months, %d days, %h hours, %i minutes and %s seconds'); // convert into Years, Months, Days, Hours, Minutes and Seconds
}
echo convertSecToTime(246395678);
//OUTPUT: 7 Years, 9 months, 21 days, 19 hours, 14 minutes and 38 seconds
?>