PHP strtotime() behavior using days of the week as frames of reference (Windows & Linux)

Say you want to get a set of timestamps so that you can query a database for all records that fall between the start of this week, and the end of this week.

One would logically try something like:


 

That will, in theory, give you a date range of Monday at 00:00:00, and Sunday at 23:59:59. Unforunately, that is not always the case.It turns out trying to find relative time references with strtotime is wonky. Depending on which day it currently is, and what day you’re trying to use as a frame of reference, you will get different results when using ‘this monday’ or ‘tuesday next week’ etc… The results are unintuitive, but more importantly, inconsistent in most cases.

Since strtotime() lets us do lazy math to get timestamps (e.g. ‘this monday – 1 week’), all we need to do is find some date string that works consistently 7 days of the week, and then we can reliably find other timestamps relative to that string. Any date string that does not have the same results for all 7 days, cannot be used as a frame of reference, as its value will change depending on what day it is!

From the tables below, we can see that the following three strings can be safely used as frames of reference in strtotime():


 

My suspicion is that certain strings consider Sunday to be the start of the week, while others consider Monday to be the start of the week, and probably round the time up or down in different ways.

Note that I have arbitrarily chosen Monday – March 4th, 2013 through Sunday – March 10th, 2013 as the week to test. Though given how wonky strtotime()’s behavior is, there’s a possibility that the behavior shown below varies from one week to the next! However, I did test various times throughout each day (e.g. 00:00:00 and 23:59:59) and thankfully the results were consistent.

So going back to our problem of getting timestamps for the start and end of this week, the cleanest and most reliable solution is this:

 

 

Using Monday as a frame of reference
If today = Mon, March 4th Tue, March 5th Wed, March 6th Thu, March 7th Fri, March 8th Sat, March 9th Sun, March 10th
strtotime(“this monday”) = March 4th March 11th March 11th March 11th March 11th March 11th March 11th
strtotime(“monday this week”) = March 4th March 4th March 4th March 4th March 4th March 4th March 11th
strtotime(“next monday”) = March 11th March 11th March 11th March 11th March 11th March 11th March 11th
strtotime(“monday next week”) = March 11th March 11th March 11th March 11th March 11th March 11th March 18th
strtotime(“last monday”) = February 25th March 4th March 4th March 4th March 4th March 4th March 4th
strtotime(“monday last week”) = February 25th February 25th February 25th February 25th February 25th February 25th March 4th
Using Tuesday as a frame of reference
If today = Mon, March 4th Tue, March 5th Wed, March 6th Thu, March 7th Fri, March 8th Sat, March 9th Sun, March 10th
strtotime(“this tuesday”) = March 5th March 5th March 12th March 12th March 12th March 12th March 12th
strtotime(“tuesday this week”) = March 5th March 5th March 5th March 5th March 5th March 5th March 12th
strtotime(“next tuesday”) = March 5th March 12th March 12th March 12th March 12th March 12th March 12th
strtotime(“tuesday next week”) = March 12th March 12th March 12th March 12th March 12th March 12th March 19th
strtotime(“last tuesday”) = February 26th February 26th March 5th March 5th March 5th March 5th March 5th
strtotime(“tuesday last week”) = February 26th February 26th February 26th February 26th February 26th February 26th March 5th
Using Wednesday as a frame of reference
If today = Mon, March 4th Tue, March 5th Wed, March 6th Thu, March 7th Fri, March 8th Sat, March 9th Sun, March 10th
strtotime(“this wednesday”) = March 6th March 6th March 6th March 13th March 13th March 13th March 13th
strtotime(“wednesday this week”) = March 6th March 6th March 6th March 6th March 6th March 6th March 13th
strtotime(“next wednesday”) = March 6th March 6th March 13th March 13th March 13th March 13th March 13th
strtotime(“wednesday next week”) = March 13th March 13th March 13th March 13th March 13th March 13th March 20th
strtotime(“last wednesday”) = February 27th February 27th February 27th March 6th March 6th March 6th March 6th
strtotime(“wednesday last week”) = February 27th February 27th February 27th February 27th February 27th February 27th March 6th
Using Thursday as a frame of reference
If today = Mon, March 4th Tue, March 5th Wed, March 6th Thu, March 7th Fri, March 8th Sat, March 9th Sun, March 10th
strtotime(“this thursday”) = March 7th March 7th March 7th March 7th March 14th March 14th March 14th
strtotime(“thursday this week”) = March 7th March 7th March 7th March 7th March 7th March 7th March 14th
strtotime(“next thursday”) = March 7th March 7th March 7th March 14th March 14th March 14th March 14th
strtotime(“thursday next week”) = March 14th March 14th March 14th March 14th March 14th March 14th March 21st
strtotime(“last thursday”) = February 28th February 28th February 28th February 28th March 7th March 7th March 7th
strtotime(“thursday last week”) = February 28th February 28th February 28th February 28th February 28th February 28th March 7th
Using Friday as a frame of reference
If today = Mon, March 4th Tue, March 5th Wed, March 6th Thu, March 7th Fri, March 8th Sat, March 9th Sun, March 10th
strtotime(“this friday”) = March 8th March 8th March 8th March 8th March 8th March 15th March 15th
strtotime(“friday this week”) = March 8th March 8th March 8th March 8th March 8th March 8th March 15th
strtotime(“next friday”) = March 8th March 8th March 8th March 8th March 15th March 15th March 15th
strtotime(“friday next week”) = March 15th March 15th March 15th March 15th March 15th March 15th March 22nd
strtotime(“last friday”) = March 1st March 1st March 1st March 1st March 1st March 8th March 8th
strtotime(“friday last week”) = March 1st March 1st March 1st March 1st March 1st March 1st March 8th
Using Saturday as a frame of reference
If today = Mon, March 4th Tue, March 5th Wed, March 6th Thu, March 7th Fri, March 8th Sat, March 9th Sun, March 10th
strtotime(“this saturday”) = March 9th March 9th March 9th March 9th March 9th March 9th March 16th
strtotime(“saturday this week”) = March 9th March 9th March 9th March 9th March 9th March 9th March 16th
strtotime(“next saturday”) = March 9th March 9th March 9th March 9th March 9th March 16th March 16th
strtotime(“saturday next week”) = March 16th March 16th March 16th March 16th March 16th March 16th March 23rd
strtotime(“last saturday”) = March 2nd March 2nd March 2nd March 2nd March 2nd March 2nd March 9th
strtotime(“saturday last week”) = March 2nd March 2nd March 2nd March 2nd March 2nd March 2nd March 9th
Using Sunday as a frame of reference
If today = Mon, March 4th Tue, March 5th Wed, March 6th Thu, March 7th Fri, March 8th Sat, March 9th Sun, March 10th
strtotime(“this sunday”) = March 10th March 10th March 10th March 10th March 10th March 10th March 10th
strtotime(“sunday this week”) = March 10th March 10th March 10th March 10th March 10th March 10th March 17th
strtotime(“next sunday”) = March 10th March 10th March 10th March 10th March 10th March 10th March 17th
strtotime(“sunday next week”) = March 17th March 17th March 17th March 17th March 17th March 17th March 24th
strtotime(“last sunday”) = March 3rd March 3rd March 3rd March 3rd March 3rd March 3rd March 3rd
strtotime(“sunday last week”) = March 3rd March 3rd March 3rd March 3rd March 3rd March 3rd March 10th

Setting up Composer globally for Laravel 4

An easy way to set up composer globally is to follow the instructions on getcomposer.org site:

Now I can use composer by invoking just the composer command.

Optional way to do it, is to set up an alias:

and

MongoDB setup to Python

Respectively; Type the following command in the terminal to install MongoDB  🙂

 

Ubuntu 14.04’e Linux, Apache, MySQL, PHP (LAMP) paketinin kurulması

LAMP Nedir?

LAMP terimini açmak gerekirse, Linux işletim sisteminde Apache Web Sunucusunun bulunması, Verilerin Mysql ile tutulması ve dinamik içeriğin PHP ile oluşturulmasıdır.

En büyük avantajlarından birisi bu dördünün de açık kaynak olmasıdır.

Bu anlatımdaki kurulum Ubuntu 14.04.1’de denenmiştir

Adım 1 – Apache Kurulumu

Apache dünyada en popüler web sunucudur. Ubuntu’nun paket yöneticisi olan apt ile kurulum yapacağımız için işimiz çok basit olacak.

Öncelikle paket veritabanını güncelleyelim

Apache2’yi kuralım

Kurulumdan sonra Apache2’nin çalışma durumunu öğrenmek için herhangi bir web tarayıcısından

adresini kullanabilirsiniz.

Adım 2 – MySQL Kurulumu

MySQL, bir veritabanı yönetim sistemidir.

Bu sefer MySQL’in yanında yardımcı(helper) paketler de kuracağız. Diğer paketlerle bağlantımızı sağlayacak.

Kurulum sırasında MySQL için şifre soracaktır.

Şimdi MySQL için biraz güvenlik önlemleri alalım. Eğer sadece geliştirme ortamı için kurulum yapıyorsanız aşağıdakileri incelemenize gerek yok diğer adıma geçebilirsiniz. Sadece production ortamı içindir.

MySQL sistem tablolarını oluşturtalım

Güvenlik amaçlı basit bir script çalıştıracağız. Girişleri olabildiğince kısıtlayacağız.

Script sırasında root şifresi isteyecektir. Kurulumda girdiğiniz şifreyi girin.

Remove anonymous users ? : Üye oluşturmaya gerek kalmadan giriş yapılmasını sağlar. Production ortamı için Yes diye cevaplayalım.

Disallow root login remotely ? : root kullanıcısının SADECE “localhost”‘dan giriş yapabilmesini sağlayacağız. Böylece root şifresi bilinse de sadece ana makineden giriş yapılabilir. Bu yüzden Yes diye cevaplayalım.

Remove test database and access to it ? : Varsayılan olarak MySQL, ‘test’ isminde bir veritabanı oluşturabilir ve herkesin erişebilmesini sağlar. Production ortamı için gereksiz olacaktır, bu yüzden Yes diye cevaplayalım.

Reload privilege tables now ? : İşlemlerin tam çalışması için yetki tablolarının güncellenmesi gerekiyor. Yes diye cevaplayalım.

Adım 3 – PHP Kurulumu

PHP, dinamik verilerin gösterimi, SQL veritabanlarından bilgilerinin alınması ve işlemlerin yönetilmesini sağlayacaktır.

PHP ile beraber helperları da yükleyelim

Apache, varsayılan olarak dizin arama işlemlerinde index.html index.cgi index.pl index.php index.xhtml index.htm dosyaları sırayla aranmaktadır. index.php‘yi biraz öne alacağız.

Bunun için dir.conf dosyasını düzenleyelim.

sudo nano /etc/apache2/mods-enabled/dir.conf index.html’den hemen sonra alabilirsiniz.

PHP’nin çalışması için Apache’nin yeniden başlatılması yani Apache’nin konfigürasyon dosyalarına tekrar bakması gerekiyor.

PHP Modüllerinin Kurulması

Yüklenebilir PHP5 Modülleri için şu şekilde arama yapabilirsiniz

Yükleyeceğiniz modülleri belirledikten sonra şöyle kurulum yapabilirsiniz

## Adım 4 – PHP’nin Test Edilmesi PHP‘yi test etmek için çok basit bir PHP kodu yazacağız.

BONUS – PHPMyAdmin Kurulumu

PHPMyAdmin’i kuralım

Web server olarak apache2‘yi seçin

dbconfig-common’un düzenleme yapmasına izin verin

MySQL ve PHPMyAdmin şifresini girin

Kurulum bittikten sonra Apache’ye PHPMyAdmin dosyasını ekleyeceğiz.

En alta Include ekleyeceğiz.

Apache’yi yeniden başlatalım

Kontrol amaçlı aşağıdaki adrese gidebilirsiniz

BONUS – Sanal Sunucu Oluşturma

Projelerinizde sürekli localhost/ kullanmak yerine projenize özel sanal sunucu oluşturabilirsiniz.

Örneğin AwesomeProject projeniz için

Çalışma Dizini: /var/www/awesomeproject/public_html

Log Dizini : /var/www/awesomeproject/logs

Site Adresi : http://awesomeproject/

Bu şekilde daha düzenli ve rahat olacaksınız.
Klasörlerin oluşturulması

Yetkilerin verilmesi

Örnek Sayfa Oluşturulması

Sanal sunucuyu test etmek için örnek bir sayfa oluşturacağız. Sanal sunucumuz içerisinde bir index.html dosyası oluşturalım.

İçerisine

Sanal Sunucu: projeadi
Sanal Sunucu Dosyasının Oluşturulması

Sanal sunucu dosyası oluşturalım.

İçerisine aşağıdakileri yapıştırabilirsiniz fakat projeadi yazdığım yerleri kendinize göre düzenleyin.

Dosyayı kaydedin ve kapatın.

Yeni sanal sunucunun çalışması için aktifleştirmemiz ve Apache’yi yeniden başlatmamız gerekiyor