Php Date and Time operations
There are some settings we need to make before we start using date data. The first of these is to tell the server which city, country or time zone we are in in order to reach the correct time and date. For this, we enter the identifier of our time zone with the “date_default_timezone_set” function. If you are curious about the time period markers of different lands, you can look here. You should write this function into your application's settings files before using the date functions.
date_default_timezone_set('Europe/Istanbul');
Select the time unit we want or We use the date function to take the format and write it to the screen. I will only mention the most used ones here
d Returns the day as two digits
m Returns the month as two digits
Y Returns the year in four-digit format
H Returns the time in two-digit 24-hour format
i Returns the minute as two digits
s Returns the second as two digits
l (small L) Gives the English name of the day of the week
F It gives the name of the month in English
According to the table above, the formats we will use to express dates and times are as follows;
echo date('d.m.Y H:i:s'); // Sample result: 29.07.2013 12:13:00
echo 'Time: ' . date('H:i'); // Sample result: Time: 12:13
echo date('d F Y l'); // Sample result: Monday, July 29, 2013
Unix time PHP uses the unix timestamp in date and time expressions. Unix timestamp, in simplest terms, is the number of seconds that have passed since "January 1, 1970 00:00:00" until today. We will use the time function to get the current unix timestamp. For example, the value I obtained according to the time I was preparing this article is as follows;
echo time();// 1375057836
You can reach the exact date by first dividing this number into seconds, minutes, hours and days, but the date function does these operations automatically for you. It is one of the cheap and high-performance ways to store the date in the database.
Time travel If you enter a value in the Unix timestamp as the second parameter to the Date function, it writes the date of the value you entered. For example, let's say we got the current time with the time function above and wrote it to the database. Then, when we want to display the date, we just need to place this value in the second parameter of the date function. This time, it will return us not the instantaneous time, but the time of the value we sent to it, as we wish.
echo date('d.m.Y H:i:s', < span class="constant numeric">1375057836); // 29.07.2013 03:30:36
echo date('H:i:s', 1375057836); // 03:30:36 ;
With the same logic, we can use an expression like this to get a minute ago;
echo date('H:i:s', time() - 60);// The time 1 minute ago appears on the screen
Creating time
In the examples above, we stamped the moment or printed the time we had previously received. The mktime function comes to get the Unix timestamp of a time we want. Its parameters are as follows: hour, minute, second, month, day, year.// 723033059
$timestamp = mktime(12, 20, 59, 11 , 29, 1992);
For example, above we obtained the Unix timestamp of 29 November 1992 12:20:59.
Calculating the time difference
Since the value of the Unix timestamp is seconds, when we subtract the two values from each other, the seconds between them are calculated. We will become aware of it. In this way, we can calculate the time between two different times. However, since the result we will get is seconds, if it is greater than 60, we need to divide it by 60 and express the minute, and if the number of minutes is greater than 60, we need to express the hour difference. For example, I created the 1992 number above. I will find the difference between the timestamp of the year and today.
$today = time();
$history = mktime(12, 20, 59, 11, 29, 1992);
$difference = $today - $past;
echo 'Today: ' . $today;
echo 'History: ' . $history;echo 'Second difference: ' . $difference;
The output I got when I tried this work was as follows; Today: 1375484072 Past: 723032459 Seconds difference: 652451613 652451613 seconds have passed between two times. This is actually the number of seconds I have lived so far. Let's divide this into time units and put it in a more understandable format.
$minutes = $difference / 60;
$second_difference = floor($difference - (floor($minute) * 60));
$hour = $minute / 60;
$minute_farki = floor($minute - (floor($hour) * 60));
$day = $hour / 24;
$hour_difference = floor($hour - (floor($gun) * 24));
$year = floor($gun/365);
$gun_farki = floor($gun - (floor($year) * 365));
echo $year . 'year';
echo $gun_difference . 'day';
echo $hour_difference . 'hour';
echo $minute_difference . 'minute';
echo $second_difference . 'seconds';
As a result of this process, we get the following result: 20 years, 251 days, 12 hours, 33 minutes and 33 seconds. That is, this is the time I have spent since the day I was born.
Converting date to unix timestamp
We can convert written date to unix timestamp with the Strtotime function. Apart from time expressions, we can also use some English expressions.
strtotime('03.08.2013 03:23');// Unix of the entered date returns the timestamp// 1375489380//
strtotime('2013-08-03');strtotime ('12:02:59');// Adds 1 day and subtracts 1 hour to the current time
strtotime('+1 day, -1 hour');// One week Returns the previous timestamp
strtotime('-1 week');// An interesting example
date('d.m.Y H:i:s', strtotime ('+5 years -2 weeks +3 months -5 minutes'));// Add 5 years, 3 months to the current time and add 5 minutes and subtracts 2 weeks, // prints the resulting time in the format I specified.
When you want your users to enter a date, you can pass the date they entered through this function and save it in your database in Unix format. If the function encounters a format it cannot understand, it will return false.
Printing the date in Turkish The Date function gives the month and days of the week in English. Therefore, when we want to print the date in Turkish, we have 2 different options. Firstly, if there is Turkish language support on the server where we will run our codes, we can achieve this with two functions.
1. Method (If Turkish language is defined in the system) First, we express the language we will use in time operations with the setlocale function. Afterwards, we get the time output in the language we express with the strftime function. The format characters of the strftime function are different from those of the date function. You can also look here for all the format characters, but I will introduce the ones we will use.
%e // Returns the day without zero padding
%m // Returns the month as two digits
%Y // Returns the year as four digits
%H // Returns the time in two-digit 24-hour format
%M // Returns the minute as two digits
%S // Returns the second as two digits
%A // Returns the name of the day of the week
%B //Gives the name of the month ex:January
We express the language we will use for time
setlocale(LC_TIME, 'tr_TR');// It can also be like this//
setlocale(LC_TIME, 'tr_TR.UTF-8'); // We print the date in the format we want and in the language we specify
echo strftime('%e %B %Y %A %H:%M :%S'); // Sample Result: Saturday, August 3, 2013 02:05:42
If your server knows Turkish, it is that easy, but if not, we must apply the alternative and somewhat manual method.
2. Method This time, we will use the date function to get the month and day of the week we are in and match the month and day names from the arrays we define. Apart from what I said above; We will use the character "N", which gives the day of the week, and "j", which gives the day without zero padding.
$days = array( 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November ', 'December');
$month = $months[date('m') - 1];$day = $days[< span class="function call">date('N') - 1]; echo date('j ') . $month . date(' Y ') . $day . date(' H:i:s'); // Sample Result: Saturday, August 3, 2013 02:19:18
If you wish, you can prepare a function for yourself using this index and expression.