Skip to content

Instantly share code, notes, and snippets.

@jamalnasir
jamalnasir / upgrage_php.txt
Created March 8, 2017 10:55
Upgrading PHP from 5.5 to 5.6
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install php5.6
a2dismod php5*
a2enmod php5.6
@jamalnasir
jamalnasir / upload.js
Created March 8, 2017 13:30
Upload file with ajax
$("body").on("submit", "#my-form", function(e){
e.preventDefault();
var _this = $(this);
var formData = new FormData(this);
$.ajax({
url: 'some-url',
data: formData,
method: 'POST',
cache:false,
@jamalnasir
jamalnasir / ago.php
Last active April 29, 2018 07:17
Ago Function in PHP
<?php
function ago($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
@jamalnasir
jamalnasir / date_formats.txt
Created March 11, 2017 09:09
PHP Date Formats
d - The day of the month (from 01 to 31)
D - A textual representation of a day (three letters)
j - The day of the month without leading zeros (1 to 31)
l (lowercase 'L') - A full textual representation of a day
N - The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
S - The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
w - A numeric representation of the day (0 for Sunday, 6 for Saturday)
z - The day of the year (from 0 through 365)
W - The ISO-8601 week number of year (weeks starting on Monday)
F - A full textual representation of a month (January through December)
@jamalnasir
jamalnasir / months_years.php
Last active April 29, 2018 07:16
Printing Months and Years
<?php
$years = [];
$months = [];
foreach(range(Carbon::now()->year - 100, Carbon::now()->year, 1) as $year){array_push($years, $year);}
foreach(range(2, 12) as $key=>$month){array_push($months, date('F', mktime(0,0,0,$month)));}
@jamalnasir
jamalnasir / strftime_date_formats.txt
Created March 13, 2017 01:36
strftime date formats
%a - abbreviated weekday name
%A - full weekday name
%b - abbreviated month name
%B - full month name
%c - preferred date and time representation
%C - century number (the year divided by 100, range 00 to 99)
%d - day of the month (01 to 31)
%D - same as %m/%d/%y
%e - day of the month (1 to 31)
%g - like %G, but without the century
@jamalnasir
jamalnasir / read_image.js
Last active March 7, 2018 11:03
Javascript Snippet to Read an Image Object
$('#file-input').change(function(e){
var files = e.target.files;
for (var i = 0, f; f = files[i]; i++) {
if(f.type){
var image_types = ['image/png', 'image/jpg', 'image/jpeg'];
if((f.type && image_types.indexOf(f.type) < 0)) {
alert('Invalid file selected. Supported formats are jpeg and png.');
$(this).val('');
} else {
readURL(f, $('.image-preview'));
@jamalnasir
jamalnasir / set_csrf.txt
Last active March 13, 2017 15:21
Setting csrf token for every ajax call
<!doctype html>
<html>
<head>
<title>Manage Contact Types</title>
<link rel="stylesheet" href="....">
<meta name="csrf-token" content="<?php echo csrf_token() ?>"/>
</head>
$(function () {
@jamalnasir
jamalnasir / add_minutes.js
Created March 19, 2017 13:13
Adding minutes to time
function addMinutes(time, minsToAdd) {
function z(n){
return (n<10? '0':'') + n;
}
var bits = time.split(':');
var mins = bits[0]*60 + (+bits[1]) + (+minsToAdd);
return z(mins%(24*60)/60 | 0) + ':' + z(mins%60);
}
@jamalnasir
jamalnasir / regex.txt
Created March 20, 2017 07:25
Regular expressions for different attributes
Email regex
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/