Mysql time and date problem

Ok, so I’ve written and re-written codes to try and compensate for my mysql date and time, the problem is my server is not the same time zone, so in order to make it local time i have to subtract an hour.

however I keep running into problems… i would love to have a local 12 hour time and date displayed next to my blog post… but this is the problem…

my time is stored as 24 hour format… and my date separate as well…

$post_time = date(‘H:i:s’);
$post_date = date(“Y-m-d”);

I can easily subtract an hour, to compensate for time difference, but i have to add little bits of code to compensate for say, 00:23:21 this is midnight in 24 hour format, but when i subtract one, it makes the time -1:23:21… so i add an if statement for that.

but that would also mean that its a day before in my time zone, so i have to subtract a day… and it just goes downhill from there… because the days in a month are not always the same, and leap years… and its really very stressful… I’ve seen tutorials on how to achieve 12 hour time. but not how to roll back days correctly in the process…

is this even possible?

is it possible to pull the local time and date directly off of my computer when I make a post, instead of using server time?

PHP should be able to do all that for you without writing code

$new_time = strtotime('-1 hour', strtotime($date . ' ' . $time));

or

$new_time = strtotime($date . ' ' . $time) - 3600;

and format it as desired

echo date('m/d/Y H:i:s', $new_time);

You could have MySQL adjust the time for you if you would store a real datetime in a single column, though. That’d be better. You can even store it in local time regardless of the server’s time zone by setting the timezone setting of the connection before issuing your insert statement. But again, that relies on having a datetime and not separating it.

I actually just figured this out and was coming back to share my findings… i did this…



$LocalTime = strtotime('-1 hours');
$LocalTime = date('Y-m-d h:i:s a', $LocalTime);


thanks for the quick response tho…

Another solution might be to use date_default_timezone_set.

Let say you’re in London and your server is somewhere in continental Western Europe (explaining the 1h difference).

Then in your php code, before any date() function, you add:


date_default_timezone_set("Europe/London");

was going to say “H” is 24 hour format while “h” is 12 hour

but seams you already figured that out :slight_smile: