Belajar Tidak Identik dengan Buku

Create a Date With PHP

Create a Date With PHP mktime()

The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used (as shown in the examples above).
The mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Syntax : mktime(hour,minute,second,month,day,year)
The example below creates a date and time from a number of parameters in the mktime() function:
Example :
<!DOCTYPE html>
<html>
<body>

<?php
$d=mktime(11, 14, 54, 8, 12, 2014);
echo “Created date is ” . date(“Y-m-d h:i:sa”, $d);
?>


</body>
</html>

Output :

Created date is 2014-08-12 11:14:54am

Create a Date From a String With PHP strtotime()

The PHP strtotime() function is used to convert a human readable string to a Unix time.
Syntax : strtotime(time,now)

The example below creates a date and time from the strtotime() function:

Example : 

<!DOCTYPE html>
<html>
<body>

<?php
$d=strtotime(“10:30pm April 15 2014″);
echo “Created date is ” . date(“Y-m-d h:i:sa”, $d);
?>


</body>
</html>

Output :

Created date is 2014-04-15 10:30:00pm
PHP is quite clever about converting a string to a date, so you can put in various values:

<!DOCTYPE html>
<html>
<body>

<?php
$d=strtotime(“tomorrow”);
echo date(“Y-m-d h:i:sa”, $d) . “<br>”;

$d=strtotime(“next Saturday”);
echo date(“Y-m-d h:i:sa”, $d) . “<br>”;

$d=strtotime(“+3 Months”);
echo date(“Y-m-d h:i:sa”, $d) . “<br>”;
?>

</body>
</html>

Output :
2014-05-12 12:00:00am
2014-05-17 12:00:00am
2014-08-11 09:56:43pm
However, strtotime() is not perfect, so remember to check the strings you put in there.

More Date Examples

The example below outputs the dates for the next six Saturdays:
<!DOCTYPE html>
<html>
<body>

<?php
$startdate=strtotime(“Saturday”);
$enddate=strtotime(“+6 weeks”,$startdate);

while ($startdate <  $enddate) {
   echo date(“M d”, $startdate),”<br>”;
   $startdate = strtotime(“+1 week”, $startdate);
}
?>

</body>
</html>


Output :

May 17
May 24
May 31
Jun 07
Jun 14
Jun 21

The example below outputs the number of days until 4th of July:
<!DOCTYPE html>
<html>
<body>

<?php
$d1=strtotime(“July 04″);
$d2=ceil(($d1-time())/60/60/24);
echo “There are ” . $d2 .” days until 4th of July.”;
?>


</body>
</html>

Output
There are 54 days until 4th of July.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Happy Paperless located at Banjarnegara , Jawa Tengah, Indonesia . Reviewed by 28 customers rated: 1 / 5



Rumus-Excel.Com
Kunjungi rumus-excel.com untuk belajar excel. Anda juga dapat menyumbangkan artikelnya ke kami untuk kita publish