Ok, i have been working on this for 2 days now and keep running into a bunch of problems. I am attempting to retreive a preset date and time from MySql in this format : 2/28/01 10:17 pm and change it to the timezone of whoever is viewing it. This particular time and date happens to be in a leap year.
My server is on Central time which is -6 hours. When a user sets their timezone, it is how many hours ahead or behind they are from me. example : -5 hours would be 1 hour ahead of me.
The problem i am having is if someone is 14 hours ahead of me, the time would be 12:17 pm and the date would be 3/1/01. I cannot get the AM and PM working correctly. and the date still stays 2/28/01.
here is my code :
<?
function getthedate($fulldate, $tmid) {
$changeam = 0;
$sql = mysql_fetch_object(mysql_query("SELECT * FROM members WHERE id = '$tmid'"));
$membertimezone = $sql->timezone; // this would be 0 for -6 hours since it is the same as my server
$datecut1 = explode(" ", $fulldate);
$datecut2 = $datecut1[0];
$timecut1 = $datecut1[1];
$amorpm1 = $datecut1[2];
$datecut = explode("/", $datecut2);
$month = $datecut[0];
$day = $datecut[1];
$year = $datecut[2];
$timecut = explode(":", $timecut1);
$hour1 = $timecut[0];
$min = $timecut[1];
$hour = $hour1 + $membertimezone;
$finalhour = $hour1 + $membertimezone;
$amorpm = $amorpm1;
$changeam = 0;
if($hour >= 12){
$finalhour = $hour - 12;
if($finalhour == 0) $finalhour=12;
if($finalhour == 24) $finalhour=12;
if($finalhour > 12) {
$finalhour = $finalhour - 12;
$changeday = 1;
}
if($finalhour != 12 && $amorpm1 == "am"){
$amorpm = "pm";
} elseif($finalhour != 12 && $amorpm1 == "pm"){
$amorpm = "am";
}
}
if($hour <= 0){
$finalhour = $hour + 12;
if($finalhour == 0) $finalhour=12;
if($finalhour <= 0){
$finalhour = $finalhour + 12;
$changeam = 1;
$changedate = 1;
$changeday = 1;
}
if($changeam != 1){
if($amorpm1 == "am"){
$changedate = 1;
$changeday = 2;
$amorpm = "pm";
}
if($amorpm1 == "pm"){
$changedate = 0;
$changeday = 0;
$amorpm = "am";
}
}
}
if($changeday==1){ // go to next day
$day = $day + 1;
if(!checkdate($month, $day, $year)){
$month = $month + 1;
$day = 1;
if(!checkdate($month, $day, $year)){
$month = 1;
$year = $year + 1;
}
}
}
if($changeday==2){ // go back a day
$day = $day - 1;
if(!checkdate($month, $day, $year)){
$month = $month - 1;
if(checkdate($month, 31, $year)) $day = 31;
if(checkdate($month, 30, $year)) $day = 30;
if(checkdate($month, 29, $year)) $day = 29;
if(checkdate($month, 28, $year)) $day = 28;
if(!checkdate($month, $day, $year)){
$year=$year-1;
$day = 31;
$month = 12;
}
}
}
$finaltime = $month."/".$day."/".$year." ".$finalhour.":".$min." ".$amorpm;
return $finaltime;
}
?>
usage : echo getthedate("2/28/01 10:17 pm", $user_id);
is there any other way i can go about doing this? i am lost in my own code 🙁
thanks in advance for the help!