Jump to content

Archived

This topic is now archived and is closed to further replies.

n8w

get the difference between two php dates

Recommended Posts

What is the best way to get the number of days between two dates in this format?
$day1=2006-07-25
$current_date=date("Y-m-d")

I have tried to subtract one from another but always get 0

Also what is the best way to get the weekday of a date?
$theweekday=getweekday(2006-07-25); //Thursday

Share this post


Link to post
Share on other sites
You could try this (got it from http://pt.php.net/datetime - see the comments)

[code]
//gregoriantojd($month,$day, $year);

$start = gregoriantojd("12", "1", "2006");
$end = gregoriantojd("12", "3", "2006");
$daysdiff = $end - $start;
echo $daysdiff;
[/code]

Share this post


Link to post
Share on other sites
Personally, i suggest storing all times and dates as timestamps, then formating them for display. This eliminates the need for more complex comparisons, and allows you to re-format the date for display in any fasion you wish.

Share this post


Link to post
Share on other sites
This should do what you need:

[code]<?php
 
$date_a = strtotime("2006-07-10");
$date_b = strtotime("2006-07-23");

$days = ($date_b - $date_a) / (60*60*24);

echo number_format($days);

?>[/code]

I also recommend you store dates as timestamps.

Share this post


Link to post
Share on other sites
HEY thanks .. works like a charm!!

I agree best to store as datetime .. and format later .. but does timestamp change to the current time everytime the record is modified

Share this post


Link to post
Share on other sites
[quote author=n8w link=topic=101570.msg402211#msg402211 date=1153694537]
but does timestamp change to the current time everytime the record is modified
[/quote]

[quote]
TIMESTAMP[(M)]

A timestamp. The range is '1970-01-01 00:00:00' to partway through the year 2037.

A TIMESTAMP column is useful for recording the date and time of an INSERT or UPDATE operation. By default, the first TIMESTAMP column in a table is automatically set to the date and time of the most recent operation if you do not assign it a value yourself. You can also set any TIMESTAMP column to the current date and time by assigning it a NULL value. Variations on automatic initialization and update properties are described in Section 11.3.1.2, “TIMESTAMP Properties as of MySQL 4.1”.

In MySQL 4.1, TIMESTAMP is returned as a string with the format 'YYYY-MM-DD HH:MM:SS'. Display widths (used as described in the following paragraphs) are no longer supported; the display width is fixed at 19 characters. To obtain the value as a number, you should add +0 to the timestamp column.

In MySQL 4.0 and earlier, TIMESTAMP values are displayed in YYYYMMDDHHMMSS, YYMMDDHHMMSS, YYYYMMDD, or YYMMDD format, depending on whether M is 14 (or missing), 12, 8, or 6, but allows you to assign values to TIMESTAMP columns using either strings or numbers. The M argument affects only how a TIMESTAMP column is displayed, not storage. Its values always are stored using four bytes each. From MySQL 4.0.12, the --new option can be used to make the server behave as in MySQL 4.1.

Note that TIMESTAMP(M) columns where M is 8 or 14 are reported to be numbers, whereas other TIMESTAMP(M) columns are reported to be strings. This is just to ensure that you can reliably dump and restore the table with these types.

Note: The behavior of TIMESTAMP columns changed considerably in MySQL 4.1. For complete information on the differences with regard to this data type in MySQL 4.1 and later versions (as opposed to MySQL 4.0 and earlier versions), be sure to see Section 11.3.1.1, “TIMESTAMP Properties Prior to MySQL 4.1”, and Section 11.3.1.2, “TIMESTAMP Properties as of MySQL 4.1”.
[/quote]

Via: http://dev.mysql.com/doc/refman/4.1/en/date-and-time-type-overview.html

Share this post


Link to post
Share on other sites
[quote author=n8w link=topic=101570.msg402211#msg402211 date=1153694537]
HEY thanks .. works like a charm!!

I agree best to store as datetime .. and format later .. but does timestamp change to the current time everytime the record is modified
[/quote]

Well, when doing the inserts into your database, you don't have to use the MySQL [color=blue]UNIX_TIMESTAMP[/color] (Incase that's what you were thinking)...what I was suggesting, was something more like:

[code]<?php

$stamp = time();
//Which would give ya a stamp up to the very second
//Or below...which would give you the same date but not as up-to-the-second
//$stamp = strtotime(date("M d, Y"));
$sql = "UPDATE table SET lastupdate=$stamp WHERE id='$user[id]' "
mysql_query($sql);

?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.