Web Hosting Talk







View Full Version : Date & Time MySQL


Northern
01-29-2006, 12:03 AM
I'm trying to build custom php sesssions, and I need an advice.
I'm trying to make it so that MySQL will remember dates as a number without dots. How do I do that?
eg: 1.4.2005 3:2:32 would be 1420053232 something like this.
Any advice?

Northern
01-29-2006, 02:59 AM
Guess I've figured it out, but then I've got another problem.
So, I use date(Ymdhms)); to get a perfect date/time without dots and lines, but it seems to have a bug or something
When I print date(Ymdhms)), it perfectly shows the current date/time.
Great...
When I put it into a variable, and then print it, it shows the date/time correct again.
But when I try to add anything to the variable, it gets messed up:

$mydate=date(Ymdhms));
echo ($mydate);
Correctly outputs something like this: 20060129080106

$mydate=date(Ymdhms));
$mydate=$mydate+5;
echo ($mydate); Outputs something like this: 2.00601290802E+013.
What's the problem? I mean, if I already have the correct number in a variable, why can't I simply add what I want to the number and get away with it?


Thanks

apex13
01-29-2006, 03:42 AM
You can't perform mathematical operations to variables that are not numbers and expect sane results

jstanden
01-29-2006, 07:06 AM
In my opinion, you shouldn't really reinvent the wheel on handling dates with your own custom format.

Date handling in PHP works like most "C-style" languages, using the number of seconds passed since the Unix Epoch (Jan 1st 1970 GMT).

That's exactly what you get back from a call to mktime() or gmmktime(), and you can pass the epoch number as an argument to date() for formatting.

That also makes it really easy to do your date arithmetic:


<?PHP

$time = mktime(3,1,0,1,29,2006);
echo date("Y-m-d H:i", $time) . "<BR>";

$add = 60 * 60 * 24 * 2; // secs * mins * hrs * days = 2 days

$time += $add;
echo date("Y-m-d H:i", $time) . "<BR>";


Hope that helps!

Northern
01-29-2006, 01:51 PM
Pretty much covered it;)
thanks everyone