Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Simple php question

Engaged ,
Mar 06, 2007 Mar 06, 2007
I want to be able to create an if/else statement for the formatting of a date cell in a dynamic result table.

This cell will turn red if the date returned is less than a specified number of days older than todays date.

The trouble is, I don't know how to subtract days from a date format....

$OldDateThreshold = (date("Y-m-d")-365); Where I am trying to set a year old threshold. - this just returns a value of 1642?

I do hate this PHP, MySQL date format!

TOPICS
Server side applications
587
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 08, 2007 Mar 08, 2007
RichardODreamweaver wrote:
> For whatever reason, if I enter a date such as 2007/02/01, it highlights ALL
> records, even after that date.
>
> If I enter 2006/02/01, it will highlight all records not updated <2007.
>
> It does not seem to be taking into account the MMDD.
>
> Does this make more sense?

Yes, it does make more sense. Without doing a lot of testing, I'm
guessing that PHP is either treating 2006/02/01 as a calculation and
producing the result 1003 (/ is the division symbol), o...
Translate
LEGEND ,
Mar 06, 2007 Mar 06, 2007
RichardODreamweaver wrote:
> The trouble is, I don't know how to subtract days from a date format....
>
> $OldDateThreshold = (date("Y-m-d")-365); Where I am trying to set a year
> old threshold. - this just returns a value of 1642?

You don't say what format the date is in, but the following gives you a
Unix timestamp for one year ago:

$yearAgo = strtotime('-1 year');

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 06, 2007 Mar 06, 2007
I am working in YYYY-MM-DD format on what I believe is UNIX

Ok - put:

$OldDateThreshold = strtotime('-year');
echo $OldDateThreshold;

Result is 1141648371 ???

Maybe it's my if/else statement that isn't right:

<?php if ($row_recordset['LASTCHECK']<$OldDateThreshold) { ?><td bgcolor="#FF0000"> <font color="#FFFFFF" font size="-2" face="Arial"> <?php echo date('D, d/m/Y',strtotime($row_mergelookup['LASTCHECK'])); ?> </font> </td>

<?php } else { ?><td> <font size="1" face="Arial"> <?php echo date('D, d/m/Y',strtotime($row_recordset['LASTCHECK'])); ?> </font> </td><?php } ?>

I know the formatting of the date in the echo statement isn't particularly "purist" but at least it works.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 06, 2007 Mar 06, 2007
RichardODreamweaver wrote:

> I am working in YYYY-MM-DD format on what I believe is UNIX
>
> Ok - put:
>
> $OldDateThreshold = strtotime('-year');
> echo $OldDateThreshold;
>
> Result is 1141648371 ???

echo date("Y-m-d",strtotime('-1 year'));

The "-1 year" is the recommended form, too.

Mick
>
> Maybe it's my if/else statement that isn't right:
>
> <?php if ($row_recordset['LASTCHECK']<$OldDateThreshold) { ?><td
> bgcolor="#FF0000"> <font color="#FFFFFF" font size="-2" face="Arial"> <?php
> echo date('D, d/m/Y',strtotime($row_mergelookup['LASTCHECK'])); ?> </font> </td>
>
> <?php } else { ?><td> <font size="1" face="Arial"> <?php echo date('D,
> d/m/Y',strtotime($row_recordset['LASTCHECK'])); ?> </font> </td><?php } ?>
>
> I know the formatting of the date in the echo statement isn't particularly
> "purist" but at least it works.
>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 06, 2007 Mar 06, 2007
RichardODreamweaver wrote:
> $OldDateThreshold = strtotime('-year');
> echo $OldDateThreshold;
>
> Result is 1141648371 ???

For somebody who spends so much time working with PHP, it's surprising
that you don't seem to know what a Unix timestamp is. It's the number of
seconds since 1 January 1970, and is the basic method of computing dates
and time in most computing environments. MySQL is different in that it
uses the ISO recommendation YYYY-MM-DD HH:MM:SS.

$OldDateThreshold = strtotime('-1 year'); gives you the Unix timestamp
for one year ago. You need to convert your MySQL date to a Unix
timestamp to compare like with like.


<?php

$OldDateThreshold = strtotime('-1 year');

if (strtotime($row_recordset['LASTCHECK'])<$OldDateThreshold) { ?><td
bgcolor="#FF0000"> <font color="#FFFFFF" font size="-2" face="Arial"> <?php
echo date('D, d/m/Y',strtotime($row_mergelookup['LASTCHECK'])); ?>
</font> </td>

<?php } else { ?><td> <font size="1" face="Arial"> <?php echo date('D,
d/m/Y',strtotime($row_recordset['LASTCHECK'])); ?> </font> </td><?php } ?>

> I know the formatting of the date in the echo statement isn't particularly
> "purist" but at least it works.

It's not perfect, but - as you say - it works. My "purist" tendencies
shudder more at your use of font tags. Time to learn CSS. ;-)

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 07, 2007 Mar 07, 2007
Thanks for this David

I'm beginning to think this might be a complex way of achieving what I want.

Would it not be easier in MySQL to have a form text field which can have a date entered (Allowing different user input). The field content could then be commited to a variable which can be used in the else/if statement.

I'm not sure how you get these contents into a variable though.

Is it something like

$Datethreshold = $HTTP_POST_VARS['Datethresholdfieldname'];

or does the later part of the statement do the job on its own?

And... I apologise for my lack of css skills. Being primarily self taught, there is still a hell of a lot to learn!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 07, 2007 Mar 07, 2007
RichardODreamweaver wrote:
> I'm beginning to think this might be a complex way of achieving what I want.

Well, it's not crystal clear what you're after.

> Would it not be easier in MySQL to have a form text field which can have a
> date entered (Allowing different user input). The field content could then be
> commited to a variable which can be used in the else/if statement.

I have no idea, because I don't really understand what you're trying to
do. However, MySQL is the database. You should store constant data in a
database, not variable data that changes with each user.

> $Datethreshold = $HTTP_POST_VARS['Datethresholdfieldname'];

That would assign to $Datethreshold the value of Datethresholdfieldname
submitted from a form.

I'm puzzled as to why you insist on using the deprecated
$HTTP_POST_VARS . It's not supported by default on PHP 5 and will be
removed completely from PHP 6. Unless you're using a really ancient (and
therefore insecure) version of PHP, you should use $_POST instead.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 08, 2007 Mar 08, 2007
My apologies for being unclear.

I have a Contact Database, within which is a field "Last Checked". When record details are checked by a user, this field is updated to today's date.

When doing mailmerges, this field is crucial as it quickly highlights those records which are likely to be out of date.

I have a page with a dynamic table from a recordset which contains all contacts to be included in a mailshot/mailmerge. In the dynamic table is the field "last checked".

What I want to be able to do is have a form field (highlight in red all those records last checked before...[ ]) with a javascript function to call up a calendarised type date/time picker. This javascript will then input the value selected (in whatever format) into the form field.

When submitted, all records that have not been updated since the threshold date will be highlighted using the else/if formatting statement.

I understand the form variable now and have just put the $_POST['datethreshold'] directly into the else/if code i.e.

<?php if ($row_mergelookup['LASTCHECK'] <$_POST['datethreshold']) { ?>.......else.....etc.

This does seem to partially work but there must still be an issue with the date formatting.

The javascript is currently using YYYYMMDD as follows:

<a href="javascript:NewCal('datethreshold','yyyymmdd')">

And it looks (when echo'd) visually like "yyyy/mm/dd"

This format does work for the majority of other recordsets in other pages where a date variable is required (such as last edited when?)

For whatever reason, if I enter a date such as 2007/02/01, it highlights ALL records, even after that date.

If I enter 2006/02/01, it will highlight all records not updated <2007.

It does not seem to be taking into account the MMDD.

Does this make more sense?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 08, 2007 Mar 08, 2007
RichardODreamweaver wrote:
> For whatever reason, if I enter a date such as 2007/02/01, it highlights ALL
> records, even after that date.
>
> If I enter 2006/02/01, it will highlight all records not updated <2007.
>
> It does not seem to be taking into account the MMDD.
>
> Does this make more sense?

Yes, it does make more sense. Without doing a lot of testing, I'm
guessing that PHP is either treating 2006/02/01 as a calculation and
producing the result 1003 (/ is the division symbol), or that it's
treating both sides of your comparison as strings. In the latter case,
even if the numbers are the same, MySQL will return the date with
hyphens, not forward slashes, so you're not comparing like with like.

What I have tested is the fact that strotime() accepts both 2006/02/01
and 2006-02-01 as valid date formats to create a Unix timestamp. So the
solution is the same as I gave you before:

<?php if (strtotime($row_mergelookup['LASTCHECK'])
<strtotime($_POST['datethreshold'])) {
?>.......else.....etc.

Computers can do a lot of calculations at lightning speed, but they're
simple creatures when it comes to logic. You must compare like with
like. You know it's a date. I know it's a date, but to a computer, it's
a bunch of numbers and symbols.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 08, 2007 Mar 08, 2007
LATEST
Works a treat - Many thanks David
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines