PHP –– Time
written by: admin
Date Written: 1/28/07
Last Updated: 9/29/13
When inserting the date and time into the MySQL database in "
datetime" format the data must be in (
YYYY–MM–DD HH:MM:SS) format ie
1999–12–31 23:59:59.
There are other formats, like
date or
timestamp, but I decided to use "
datetime", because it was easy for me to understand and still kept track of the time last updated down to the second. The problem was that I wanted it to display the time in a different format than what was stored in the database. I wanted the timestamp to be displayed without the hours, minutes, and seconds being displayed; just the year, month day, like 2/27/09.
To do this I set the column named '
date' in a table in my MySQL database to format '
datetime'. I then retrieved the data for the "
date last updated" field and assign it a string value
$date2 I then removed the colons, hyphens, and spaces using the
str_replace(a,b) function so that it is only a string of numbers 14 digits long.
reformats datetime into readable format
$date=date("YmdHis", strtotime("now"));
$date=str_replace(" ","",$date);
$date=str_replace(":","",$date);
$date=str_replace("-","",$date);
$y1=substr($date,2,2);
$m1=substr($date,4,2);
if ($m1<10) $m1=substr($m1,1,1);
$d1=substr($date,6,2);
if ($d1<10) $d1=substr($d1,1,1);
$sun="am";
$h1=substr($date,8,2);
if ($h1<10) $h1=substr($h1,1,1);
if ($h1>=12) {$h1=$h1-12; $sun="pm";}
if ($h1<1) $h1=12;
$min1=substr($date,10,2);
echo "$m1/$d1/$y1 $h1:$min1$sun";
output
12/21/10 8:51pm
A countdown script
<?php
$second = 0;
$minute = 0;
$hour = 0;
$day = 30; // Day of the countdown
$month = 3; // Month of the countdown
$year = 2009; // Year of the countdown
$target = ceil(mktime($hour,0,0,$month,$day,$year));
$diff = $target - time();
$days = ($diff - ($diff % 86400)) / 86400;
$diff = $diff - ($days * 86400);
$hours = ($diff - ($diff % 3600)) / 3600;
$diff = $diff - ($hours * 3600);
$minutes = ($diff - ($diff % 60)) / 60;
$diff = $diff - ($minutes * 60);
$seconds = ($diff - ($diff % 1)) / 1;
echo "There are $days days, $hours hours, $minutes minutes, $seconds seconds left till March 12, 2009";CODE
With a minor modification you can do the reverse and show how many days since a particular date.
CODE<?php
$second = 0;
$minute = 0;
$hour = 0;
$day = 5; // Day of the countdown
$month = 3; // Month of the countdown
$year = 2009; // Year of the countdown
$target = ceil(mktime($hour,0,0,$month,$day,$year));
$diff = $target - time();
$days = ($diff - ($diff % 86400)) / 86400;
$diff = $diff - ($days * 86400);$days=$days*(-1);
$hours = ($diff - ($diff % 3600)) / 3600;
$diff = $diff - ($hours * 3600); $hours=$hours*(-1);
$minutes = ($diff - ($diff % 60)) / 60;
$diff = $diff - ($minutes * 60); $minutes=$minutes*(-1);
$seconds = ($diff - ($diff % 1)) / 1; $seconds=$seconds*(-1);
echo "There are $days days, $hours hours, $minutes minutes, $seconds seconds left since March 12, 2009";
?>
You may need to modify your internet time to your own local timezone. see below.
Days remaining in the year
<?php
$leap=date("L");$year=date("Y");
$g=time()-mktime(0,0,0,1,1,$year);$g=floor($g/86400);
if ($g==0) $g=1;$gg=365-$g;if ($leap==1) $gg=366-$g;
echo "Day $g of this year. There are $gg days of the year remaining."
?>
internet time
$today = date("F j, Y, g:i a");
echo "Internet time is $today."
display 4 most recent results
SELECT ID, col, date FROM tablename ORDER BY date DESC LIMIT 0, 4
set internet time to local time
$webtime is entered in by the user and is usually in the seconds format, so 3600 is for each hour or 7200 for 2 hours or you can use "1 hour" or "2 hours" otherwise known as plain english.
$webtime="1 hour";
$e = strtotime("now + $webtime");
$today = date("F j, Y, g:i a", $e);
print "My time here in Iowa is $today.";
TIMESTAMPDIFF()
This will display the difference between review_date and now in months. Other valid intervals include second, minute, hour, day, month, quarter, and year.
<?php
$query = "SELECT TIMESTAMPDIFF(MONTH,review_date, now()) FROM tablename WHERE ID=16;";
$test = mysql_query($query);
$test1 = mysql_fetch_array($test,MYSQL_ASSOC);
$test2 = array_values($test1);
$test3 = $test2[0];
echo "$test3 months.";
?>
microtime()
<?php
$time=microtime();
// script goes here
$time2=microtime();
$totaltime=$time2-$time;
echo "total time was $totaltime".".";
?>
Just a handy way of checking how long it takes to run a given script.
Articles from the last 10 days
SELECT ID, date FROM tablename WHERE date > CURDATE()-10;
This displays results that are not more than 10 days old.
links
Here is a link to the
time page where some examples of code can be seen in action.
php date formattingTAGS: mysql,
php