Skip to content

Instantly share code, notes, and snippets.

View mirfan's full-sized avatar
💭
I may be slow to respond.

Mohammed Irfan mirfan

💭
I may be slow to respond.
View GitHub Profile
@mirfan
mirfan / season_from_date.php
Created February 20, 2024 11:33
Get season name from date. A modernized version of https://stackoverflow.com/a/7928049
function getSeason(string $date): string {
$seasons = ['Winter', 'Spring', 'Summer', 'Fall'];
$timestamp = strtotime($date);
$date_year = date('Y', $timestamp);
return match (true) {
$timestamp < strtotime($date_year . '-03-21') || $timestamp >= strtotime($date_year . '-12-21') => $seasons[0],
$timestamp >= strtotime($date_year . '-09-23') => $seasons[3],
$timestamp >= strtotime($date_year . '-06-21') => $seasons[2],
<?php
function distance($lat1, $lon1, $lat2, $lon2) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
return $miles;
}
<?php
function roundToTheNearestAnything($number) {
return (floor( $number / 100 ) * 100) / 10;
}
@mirfan
mirfan / Auth.php
Created June 26, 2014 16:47
Basic Auth library for CodeIgniter
<?php
class Auth {
private $ci;
public function __construct() {
$this->ci = &get_instance();
}
public function login($username) {
$this->ci->session->set_userdata('username', $username);
@mirfan
mirfan / git-config-example
Created January 25, 2014 12:30
git config example
# -*- sh -*-
[user]
name = Mohammed Irfan
email = mirfan@live.in
[github]
user = mirfan
[core]
@mirfan
mirfan / fp_parse_url.js
Created November 16, 2013 11:53
Parse url parameters
/**
* mirroring code from http://www.reddit.com/r/javascript/comments/1qpjrz/succinct_one_liner_for_parsing_url_parameters/cdfjaix
*/
location.search.slice(1)
.split('&')
.map(function (s) {return s.split('=')})
.filter(function (a) {return a[1]})
.reduce(function(d, e){
d[e[0]] = e[1];
return d;
@mirfan
mirfan / agile-git.sh
Created November 11, 2013 13:21 — forked from nz/agile-git.sh
# zsh shell functions for working in local git branches and rebasing appropriately
function rebase {
if [[ $1 == "" ]]; then master='master'; else master=$1; fi
branch=`git branch | grep \* | awk '{print $2}'` &&
git co $master && git pull && git rebase $master $branch
}
function push {
if [[ $1 == "" ]]; then master='master'; else master=$1; fi
@mirfan
mirfan / flatten_multi_level_arr.php
Last active February 20, 2024 11:49
Flatten a multi level array (updated to use features from PHP 8.3.x)
<?php
/**
* Courtesy of: http://stackoverflow.com/a/17695576
*/
// Reduces one level
$concat = fn($x) => array_reduce($x, 'array_merge', []);
// We can compose $concat with itself $n times, then apply it to $x
// This can overflow the stack for large $n
@mirfan
mirfan / index.php
Created February 4, 2013 03:38 — forked from Chase-san/index.php
<?php /* Copyright(c) 2013 Robert Maupin. Released under the ZLIB License. */
if(count($_FILES) > 0) {
extract($_FILES['file']);
list($w,$h,$type)=getimagesize($tmp_name);
/*see exif-imagetype() documentation :) */
if(!$type||$type>3||filesize($tmp_name)>1024*200)
exit();
$ext=image_type_to_extension($type,false);
$md5=md5_file($tmp_name);
move_uploaded_file($tmp_name,$n="img/$md5.$ext");