PDA

View Full Version : PHP Date Script



Kame
20-05-2003, 12:25 AM
I have a php script

<?php $time = date("g:i a"); PRINT "

Time is $time</P>"; ?>

What it does is prints out on a webpage

e.g. Time is 6:23 am

I need to adjust the time as it's 16 hours behind. How can I do this?

Merlin
20-05-2003, 01:04 AM
Easy

Learn here (http://www.phpbuilder.com/manual/function.date.php)

Stumped Badly
20-05-2003, 10:20 AM
Very handy site Merlin
Thanks

Kame
20-05-2003, 12:02 PM
Although the site was ok for starters, and I like gmtime() except am and pm is still shown incorrectly.

I've done the script but I can't convert am to pm or vice versa. date("a") just shows what I think is the servers time.

So far I've got the script as (was a CGI script I made for someone last year, which I've removed all day variables e.g. Monday 20th)

<?php
$hour = date("H"); // hours in 24hr format
$min = date("i"); //minutes
$mday = date("a"); // am or pm
if ($hour <= 7)
{
$hour = $hour + 16;
}
elseif ($hour >= 8)
{
$hour = ($hour - 8);
// $mday = (code to change am/pm)
}
PRINT "$hour:$min $mday
"; // the adjusted time
echo date("g:i a"); // shows what the actual server time is
?>

Merlin
21-05-2003, 11:24 AM
It sounds like the server is in one timezone and the date/time is required from another.

Look here (http://www.phpbuilder.com/board/showthread.php?s=e647c637fec5315efc42de8a20cdae43&threadid=10239681&highlight=local+date+and+time)

Kame
21-05-2003, 12:06 PM
Cheers Merlin,

I think I'll just add function to figure out whether $hour is am or pm. Although it's not required in a 24hour format, but I'll fix that up too.

Kame
21-05-2003, 12:32 PM
Here's the PHP Script that I've got working correctly, although it seems a bit big for just a small script, would appreciate it if there's a way I can simplify it.

<?php
$hour = date("H"); // 24hrs
$min = date("i"); // minutes
$mday = ""; // am/pm variable

if ($hour <= 7) {
$hour = $hour + 16;
}
elseif ($hour >= 8) {
$hour = ($hour -8);
}

if ($hour == 12 || $hour > 12) {
$mday = "pm";
}
elseif ($hour == 24 || $hour < 12) {
$mday = "am";
}
if ($hour == 24 || $hour > 12) {
$hour = $hour - 12;
}

PRINT "$hour:$min $mday
"; // localtime hopefully
echo date("H:i a"); // servers time
?>

Merlin
21-05-2003, 03:26 PM
Try this

<?php
//get the current date and time
$now = date ("d-m-Y h:i:s a");

// get the date and time 16 hours ago
$timethen = mktime (date("h")-16,date("i"),date("s"),date("m"),date("d"),date("Y"));


print $now; // print current date and time
print " - server or current date and time
";
print date ("d-m-Y g:i:s a","$timethen"); // print the date and time 16 hours ago
print " - local or earlier date and time
";
?>

Kame
22-05-2003, 12:19 AM
Cheers again Merlin,

A bit too much info there but the more detail the better.

I've fixed it up to now just

<?php
$mytime = mktime(date("h")+16); // give the server a push
print date("g:i a","$mytime");
?>

I'll now go and research which is better to use echo or print as they both do the same thing.