PHP Tutorial #27 The Date ( timestamp, date, timezone and strtotime )

in #utopian-io6 years ago (edited)

Image Source

Repository

https://github.com/php/php-src  

What Will I Learn?

  • You will learn the timestamp.
  • You will learn how to get the date with format using the date() method.
  • You will learn how to get and set the timezone.
  • You will learn the StrToTime() method.

Requirements

System Requirements:
OS Support for PHP
  • Windows
  • macOS
  • Linux

Required Knowledge

  • HTML language
  • CSS language

Difficulty

  •  Intermediate 

Description

In this tutorial we will learn the dates and times, there is no thing almost without the notion of time " publications, comments ..etc " almost all the tasks you can specify it using the dates and the time.

Dates in PHP have always been a fundamental issue for developers, how to store, manage, manipulate dates or time intervals, most developers will say : " the timestamp ".

Today we are going to see the function date(), is not the object date because we are going to learn the objects in the next tutorials, and we also have the timestamp, the function strtotime() and finally the timeZone set and Get.

1- TimeStamp

In PHP to use date the start was the timestamp, the timestamp is the number of seconds that have passed since January 1, 1970,  the timestamp was at the base everything in all languages. We didn't complain too much the web was not too developed, manage dates in PHP was very common (for forums) but was not very developed.

With time, the timestamp has become longer and longer, with the development of the webthe use of dates is become more frequent and more complex.

Relational database managers such as " MySQL " or " Postgres " had already shown the power of a datatype specific to dates rather than the timestamp itself, in particular because it allowed to keep only the part "date" or the part "time" ( hours, minutes, seconds).

2- Date Method

The function 'date' forms or returns the date and local time of your server, it returns a date in the form of a string in the format given by the format parameter.

The most important thing is that this function must contain a parameter, you must indicate the display or value returned by this function.

The function contains the format as parameter, the format offered by PHP with several characters.

This function accepts a second argument that corresponds to a number of seconds since the beginning of the UNIX epoch.

3- TimeZone

One of the big problems when you want to manage the hours is that everyone on earth is not at the same time. Between time zone and summer time you will quickly fall into difficult problems.

You aren't unaware that on our good old Earth there are 24 time zones,these range from -12 to +12 through Greenwich, in order to make sure that each of your users visualize your site at its own time, you must set the time zone of your site using Timezone of php.

We will see the " date_default_timezone_get() " and " date_default_timezone_set(timezone) " functions.

The "date_default_timezone_set(timezone) "  will change the default timezone, if the default timezone is Europe/Berlin, it will be with the timezone that you propose.

The " date_default_timezone_get() " will retrieve the default timezone from your system, to see and fix the problem we mentioned.

4- StrToTime Method

The " strtotime() " function tries to read a date in US English format in the time string, and turn it into a Unix timestamp.

Really the use of the timestamp is a little difficult on all if you want a date in the past, for that PHP offers this solution you can enter a date like " last year " for example in English, or something that has a relationship with time ( the time indectors we can say ) , and this function will try to understand it and return the date in place of a succession of digits that it forms the timestamp.

It accepts two parameters :

The first is the time : it's required specifies a date/time string to parse.

The second is now: it's optional, specifies The timestamp representing the current date, used for the relative calculation of dates.

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/date.php

Sort:  

Hi @alexendre-maxim

This is another great addition of video tutorial to your series.

Your presentation is clear and to the point.

As for the written text, try to write short simple sentences. People who have English as their second language tends to write run on sentences without putting in the proper punctuation.

Long run on sentences make it difficult for people to follow the text.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @rosatravels! Keep up the good work!

Thank you @rosatravels, Yes to understand better, sentences must be simple.

Hi @alexendre-maxim!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @alexendre-maxim!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!