CodeIgniter Forums
PHP Questions About Date - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: PHP Questions About Date (/showthread.php?tid=8823)

Pages: 1 2


PHP Questions About Date - El Forum - 06-02-2008

[eluser]CodeIgniterNewbie[/eluser]
1. What is the proper way to create a date/time object in PHP to represent dates prior to the Unix Epoch? Some of the examples I've seen seem to depend on MySQL's date handling. Let's assume MySQL (or any other databases) is not part of the application; so, a pure PHP solution is required.

2. What is the proper way to do date/time comparisons (including date/time prior to Unix Epoch)? Note that I want to do "true" date/time comparisons and not have to depend on a string formatting for a solution (e.g. you can say that the string "20080101" < "20080201". This isn't a true date/time comparison, though. It relies on the format yyyymmmdd.)


PHP Questions About Date - El Forum - 06-02-2008

[eluser]Seppo[/eluser]
1. PHP uses negatives timestamp. Under unix it works like a charm, but I think this raises errors under Windows.

2. Timestamp again.


PHP Questions About Date - El Forum - 06-02-2008

[eluser]CodeIgniterNewbie[/eluser]
Negative timestamps, huh? Got snippet?


PHP Questions About Date - El Forum - 06-02-2008

[eluser]Seppo[/eluser]
Code:
strtotime('1969-11-02'); // returns -5173200

-Edit- you might get some different number according your GMT settings


PHP Questions About Date - El Forum - 06-02-2008

[eluser]CodeIgniterNewbie[/eluser]
So, -5173200 is the integer representation of November 11, 1969? Sounds simple enough. I feel a little more confident doing integer comparisons to compare dates than doing string comparisons. Will test this out. Thanks.


PHP Questions About Date - El Forum - 06-02-2008

[eluser]Sean Murphy[/eluser]
You'll still have a problem representing dates earlier than 1901 using timestamps though.


PHP Questions About Date - El Forum - 06-02-2008

[eluser]CodeIgniterNewbie[/eluser]
1. Given -5173200, how do I know what year, month, and day that is?

2. Dates prior to 1901: I don't have a need for dates prior to 1901, how would I handle them if I did have a need?


PHP Questions About Date - El Forum - 06-02-2008

[eluser]Seppo[/eluser]
1. With the date function, ie date('Y-m-d, -5173200); will give you the MySQL format

2. That's thought... probably string, or you might use this way but it will be casted as float and your dates won't be exact


PHP Questions About Date - El Forum - 06-02-2008

[eluser]CodeIgniterNewbie[/eluser]
That is very concerning about dates prior to 1901. How is this handled in a none "pure" PHP way?


PHP Questions About Date - El Forum - 06-02-2008

[eluser]megabyte[/eluser]
I would recomment storing dates in a mysql database as a "date" not a timestamp. There is no negative issues, you can format the output anyway you want to, cpmpare it anyway you want to.

There are really zero negative issues with it at all.

If you need date and time, just use the "date time".

I'd actually love to hear some arguments for timestamp against date/datetime. not that I think I'm right. I just havent been able to find any.