Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 05-05-2009
Member
 
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
>
Reply With Quote
  #2  
Old 05-05-2009
Member
 
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.
Reply With Quote
  #3  
Old 05-05-2009
Member
 
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);
>
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 03:00 PM.