Forum Home
Press F1
 
Thread ID: 33584 2003-05-19 12:25:00 PHP Date Script Kame (312) Press F1
Post ID Timestamp Content User
145819 2003-05-19 12:25:00 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?
Kame (312)
145820 2003-05-19 13:04:00 Easy

Learn here (www.phpbuilder.com)
Merlin (503)
145821 2003-05-19 22:20:00 Very handy site Merlin
Thanks
Stumped Badly (348)
145822 2003-05-20 00:02:00 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
?>
Kame (312)
145823 2003-05-20 23:24:00 It sounds like the server is in one timezone and the date/time is required from another.

Look here (www.phpbuilder.com)
Merlin (503)
145824 2003-05-21 00:06:00 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 (312)
145825 2003-05-21 00:32:00 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
?>
Kame (312)
145826 2003-05-21 03:26:00 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
";
?>
Merlin (503)
145827 2003-05-21 12:19:00 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.
Kame (312)
1