Results 1 to 3 of 3

Thread: Date function in PHP

  1. #1
    Join Date
    May 2008
    Posts
    29

    Date function in PHP

    The management of dates in PHP is very simple. the function date (). We then see the time () and mktime ().

    Function date ()

    The function date () is used in different ways. It always returns a string corresponding to what he had requested (it may be the time, day, month, etc. ). Attention, the parameters correspond to those returned from the server, if your server has a disorderly hours, the function date () does not return correct time.

    Example to view the current year with the function date ():

    PHP Code:
    <? php
    echo date 'Y') / / display the current year in 4 digits
    The <? Php
    echo date 'Y') / / display the current year in 4 digits
    >
    You just have to look at other codes to see what you want. It's all well and good but if you want to display a date with a single call to the function date, how would you do?

    If you want to display the months and years, you could do this:

    PHP Code:
    <? php
    echo date 'm'). " '. date ( 'Y') / / display the month on two digits and the current year in 4 digits
    > And the <? Php
    echo date ( 'm'). " '. date ( '
    Y') / / display the month on two digits and the current year in 4 digits
    >
    However, this solution is impractical and greedy. You have a much easier way to do this:

    PHP Code:
    <? php
    echo date 'm Y') / / display the month on two digits and the current year in 4 digits
    The <? Php
    echo date 'm Y') / / display the month on two digits and the current year in 4 digits
    >

  2. #2
    Join Date
    May 2008
    Posts
    29

    Re: Date function in PHP

    PHP timestamp

    A timestamp is a number that indicates the number of seconds that have elapsed since the first in January 1970. The first in January 1970 to midnight, the timestamp was 0. It now holds 10 numbers, ie if the number of seconds that have elapsed since is important!

    To display the timestamp of the second present, it uses time () function of PHP. Here's an example:


    PHP Code:
    <? php
    echo time () / / Displays the number of seconds elapsed since 1 January 1970
    >
    By default, the date function requires only one parameter as we have seen above, this allows for example to display the current day or the minute and the second current. We can against and then it becomes interesting to see all this information for a given timestamp. The date function as we have seen is this:

    PHP Code:
    <?php 
    echo date 'Y' time ()); 
    ?>
    If we indicate different timestamp, PHP will return whatever you want for the timestamp, and not for the current time.

    PHP Code:
    <? php
    timestamp time () - 3600;
    echo 
    date 'j', $ timestamp);
    >
    PHP will return here the day it was an hour ago! indeed, it uses the timestamp $ timestamp that matches the current timestamp.

  3. #3
    Join Date
    May 2008
    Posts
    29

    Re: Date function in PHP

    Conversions from one date to a timestamp

    PHP allows you to convert a date to a timestamp. To do this, we use the function mktime (). It will allow the date, time, etc ... a classic date, to obtain the corresponding timestamp. This timestamp can then be stored or further processing. Here is the basic syntax of the function mktime ():

    PHP Code:
    <? php
    timestamp mktime (hoursminutessecondsmonthdayyear);
    >
    If you want to get the timestamp of January 3, 1988 at 2:31 and 10 seconds, you can put this:

    PHP Code:
    <? php
    echo mktime (23110131988);
    >

Similar Threads

  1. Sql query for Date function.
    By horuy in forum Software Development
    Replies: 3
    Last Post: 17-11-2010, 12:22 AM
  2. Discussion of Date and Time function
    By Gerri in forum Software Development
    Replies: 3
    Last Post: 29-01-2010, 12:03 PM
  3. Date function in php including milliseconds
    By hamX15 in forum Software Development
    Replies: 3
    Last Post: 22-06-2009, 02:53 PM
  4. MySQL date & time function.
    By Amaresh in forum Software Development
    Replies: 2
    Last Post: 19-06-2009, 02:49 PM
  5. Problem with Php date function
    By HAMAKO in forum Software Development
    Replies: 1
    Last Post: 21-04-2009, 12:38 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,258,963.07928 seconds with 17 queries