TIME NOW
World current time now,
CALENDAR
Calendar monthly, yearly
login CONVERT LENGTH
login CONVERT TEMPERATURE
login DICTIONARIES, LISTS
login SCIENCE EDUCATION RELIGION
login WORK CALCULATOR
login CALCULATE LIFE

PHP scripts, code, examples, tutorial

Previous articlePage bottomNext article  ALL TOPICS

PHP. Date, time, timestamp functions, examples, code

 
Example 1. date to timestamp:

<?php

$indate= '2009-07-24';
cinverttimestamp($indate);

function cinverttimestamp($indate){
list($year, $month, $day) = explode('-', $indate);
$timestamp = mktime(0, 0, 0, $month, $day, $year);
return $timestamp;
}

?>

Example 2. date to timestamp:
<?php
$timestamp = strtotime('22-09-2008');


list($day, $month, $year) = explode('-', '22-09-2008');
echo mktime(0, 0, 0, $month, $day, $year);


?>

Example 3. timestamp depends on time zone:
<?php

function dateToTimestamp($date, $format, $timezone='Europe/Belgrade') {

//returns an array containing day start and day end timestamps

$old_timezone = date_timezone_get();

date_default_timezone_set($timezone);

$date = strptime($date,$format);

$day_start = mktime(0,0,0,++$date['tm_mon'],++$date['tm_mday'], $date['tm_year']+1900));

$day_end=$day_start+(60*60*24);

date_default_timezone_set($old_timezone);

return array('day_start'=>$day_start, 'day_end'=>$day_end);

}

$timestamps = dateToTimestamp('15.02.1991.', '%d.%m.%Y.', 'Europe/London'); $day_start  = $timestamps['day_start'];

?>

Example 4. strtotime function:
<?php

echo strtotime("now"), "\n<BR>";

echo strtotime("10 September 2000"), "\n<BR>"";

echo strtotime("+1 day"), "\n<BR>"";

echo strtotime("+1 week"), "\n<BR>"";

echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n<BR>"";

echo strtotime("next Thursday"), "\n<BR>"";

echo strtotime("last Monday"), "\n<BR>"";

?>

int time  ( void  ) Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
<?php

time();

?>

strftime- Format a local time/date according to locale settings
string strftime ( string $format [, int $timestamp = time() ] )
Format:
%s Unix Epoch Time timestamp (same as the time()  function)    

Example: 305815200 for September 10, 1979 08:40:00 AM
<?php

strftime ("%s")

?>

Date from timestamp:
<?php
echo date("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 1998));
echo gmdate("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 1998));
?>
 
Previous articlePage topNext article  ALL TOPICS



 Use username: Guest, Anonymous, Programmer






QUOTES:
It's very easy to forgive others their mistakes, it takes more gut and gumption to forgive them for having witnessed your own.
Jessamyn West
A lady is nothing very specific. One man's lady is another man's woman; sometimes, one man's lady is another man's wife. Definitions overlap but they almost never coincide.
Russell Lynes
While gossip among women is universally ridiculed as low and trivial, gossip among men, especially if it is about women, is called theory, or idea, or fact.
Andrea Dworkin