Welcome Guest, Not a member yet? Register   Sign In
Date and time help!
#1

[eluser]Assim[/eluser]
I wanted to ask a few stuff about dates and time.

I'm storing dates and times in my MySQL database in datetime format (YYYY-MM-DD HH:MM:SS). But now I have two questions.

- How can I store the current date and time in GMT+4 format?
- How can I retrieve the current date time but without taking it from my server time? Maybe some API?

Maybe these two questions are connected somehow but if if you have the answer of one question, then it will be very helpful.
#2

[eluser]dmorin[/eluser]
Somewhere in your app, you should be setting your default timezone (http://php.net/manual/en/function.date-d...ne-set.php). If you are setting your default timezone to GMT+4, then any date you create will be in that format already. If you're setting it to something else, check out the DateTime Class (http://www.php.net/manual/en/class.datetime.php). When you instantiate the class, it'll be set to the current timezone. You can then use DateTime:ConfusedetTimezone to set it to your target timezone to get GMT+4.

Why don't you want to get the current date time from your server? If your server's clock is off, you should be syncing it with a time server to make sure it's correct, otherwise the timestamps in your logs will all be off also.
#3

[eluser]Assim[/eluser]
[quote author="dmorin" date="1266991655"]Somewhere in your app, you should be setting your default timezone (http://php.net/manual/en/function.date-d...ne-set.php). If you are setting your default timezone to GMT+4, then any date you create will be in that format already. If you're setting it to something else, check out the DateTime Class (http://www.php.net/manual/en/class.datetime.php). When you instantiate the class, it'll be set to the current timezone. You can then use DateTime:ConfusedetTimezone to set it to your target timezone to get GMT+4.

Why don't you want to get the current date time from your server? If your server's clock is off, you should be syncing it with a time server to make sure it's correct, otherwise the timestamps in your logs will all be off also.[/quote]
I still didn't get it, when do I use those function and where, or could you give me an example on how to use it in my app?

I thought if I move from server to server, the time zone changes then I will get the wrong time unless I update.

But if you have any suggestion on how to get the dates and times from the server in a better way, then please advise.
#4

[eluser]dmorin[/eluser]
Quote:I still didn’t get it, when do I use those function and where, or could you give me an example on how to use it in my app?
It's going to be tough to give you an example, when you're question was so generic. You haven't told us at all what you're doing.

Quote:I thought if I move from server to server, the time zone changes then I will get the wrong time unless I update.
That's why for all of the servers I use, I set the timezone to UTC both in PHP and in MySQL. That way, I'm always storing them consistently. If you're worried about changing DB servers and that giving you bad times because of the way MySQL stores dates and times, then just store unix time stamps as integers instead since these are always in UTC and it's easy to convert them to and from any format or timezone.

If you need any specific suggestions, please thoroughly explain what you're doing first and then we'll see what we can come up with.
#5

[eluser]Assim[/eluser]
What I'm trying to do is store date and time for different things like user posts, or pages, or events, and everything and anything that requires date and time. So for the example, let's say a user posts a post in my website, how can I store the date and time?

[quote author="dmorin" date="1267041223"] If you're worried about changing DB servers and that giving you bad times because of the way MySQL stores dates and times, then just store unix time stamps as integers instead since these are always in UTC and it's easy to convert them to and from any format or timezone.[/quote]
That seems to be a reasonable solution, could you tell me how I do the following:
- Store in unix timestamps
- Convert them to any format or timezone
- Comparing date and times

If explaining was hard, do you have any newbie guide for the using unix timestamps in PHP and MySQL (one that you recommend)?

Thanks.
#6

[eluser]dmorin[/eluser]
A unix timestamp is just an integer (number of seconds since Jan 1 1970 00:00:00). So store it in your database as an unsigned integer (or an unsigned bigint if you're hoping to still be using this same app in a few decades). You can get a unix timestamp using time().

Code:
$time1 = time(); //$time1 now contains a number
sleep(2);
$time2 = time(); //$time2 now contains a number, 2 larger than $time1
print_r($time1 < $time2); // should show true since $time1 is before $time2

echo date('Y-m-d H:i:s e', $time1); //will format $time1 into the Y-M-D H:M:S tz format.  See  http://us2.php.net/manual/en/function.date.php for more formatting.
#7

[eluser]danmontgomery[/eluser]
IMHO, time is best stored in MySQL in a datetime field. It's easier to use MySQL's date functions that way (NOW() especially) , they are more easily readable, still comparable in MySQL the same way as unix timestamps, and to use them in PHP's date() you just need to pass them through strtotime(). Really up to you as a developer though, many people prefer unix timestamps.
#8

[eluser]dmorin[/eluser]
@noctrum

I knew someone was going to post about having an opinion between timestamps and datetime values Wink.

Quote:It’s easier to use MySQL’s date functions that way (NOW() especially)
MySQL has functions for dealing with each kind of time representation so this isn't really true.

I think it's all situational. I never use NOW() in my queries because I want a single source for defining times, and that's almost always my application. If you start setting times in both places, and they're on different servers, you can end up with inconsistant data. Since I'm not always setting the current time, that means I have to set times in my application so I choose to set all times in my application.

But the real reason I generally prefer timestamps is because they eliminate all complexity with timezones. MySQL converts to and from timezones automatically when storing and retrieving a datetime field. With unix timestamps, it doesn't matter if your php install is using one timezone, your server another, and your db another, it just works. That said, it's obviously much easier to look at a datetime formated string instead of a timestamp, however, MySQL does have a function that will do that conversion for you, so that's not a big deal either.
#9

[eluser]Assim[/eluser]
[quote author="dmorin" date="1267054733"]A unix timestamp is just an integer (number of seconds since Jan 1 1970 00:00:00). So store it in your database as an unsigned integer (or an unsigned bigint if you're hoping to still be using this same app in a few decades). You can get a unix timestamp using time().

Code:
$time1 = time(); //$time1 now contains a number
sleep(2);
$time2 = time(); //$time2 now contains a number, 2 larger than $time1
print_r($time1 < $time2); // should show true since $time1 is before $time2

echo date('Y-m-d H:i:s e', $time1); //will format $time1 into the Y-M-D H:M:S tz format.  See  http://us2.php.net/manual/en/function.date.php for more formatting.
[/quote]
It's easier than I thought it would be, so simple, and the solution to my problem.

To express my gratitude:
Code:
$i = 0;
$end_point = pow(100, 100);
while($i < $end_point)
{
echo "Thank you<br>";
$i++;
}




Theme © iAndrew 2016 - Forum software by © MyBB