Skip to main content
Inspiring
April 25, 2006
Question

Date Formatting

  • April 25, 2006
  • 15 replies
  • 729 views
I need some help formatting some dates. They're extremely hard for me to
get my head around, for some reason.

I am currently working on this page:
http://demo.mediachurch.com/calendarlist.php (above the hr)

What I am trying to do is capture today's date and compare it with my stored
date in my calendar. The stored date in the calendar is a Serial Date + a
constant. So I need to capture the time, change it to a Serial date, and
then add my constant to compare it to the database.

However, at the moment I'm having a hard time just capturing and formatting
today's date, as you can see. In fact, I'm having a hard time just getting
my formatting script to work as it keep spitting out the same date.
*However* the frustrating part is that my script works to pull out and
format the dates from the calendar (see below the <hr> .

Here's what I have:

MY TWO RECORDSETS (I can duplicate if you need them)

MY FUNCTIONS AND VARIABLE DEFINITIONS

//VARIABLES
//Get Today's Date
$priivaDateToday = time();
$priivaTestDate = $row_RECORDSETSTUFF


define('EPOCHINEXCEL', 25568); //01-01-1970 minus 1 Defines the serial
date of 1970, which should ensure the script works on windows machines

//This changes the date from MS (days since 1900) to Unix (seconds since
1970)
function priivaExcel2Unix($n) {
return mktime(0, 0, 0, 1, $n - EPOCHINEXCEL, 1970);
}

//This changes the date from Unix to Excel
function priivaUnix2Excel ($n) {
return mktime (0, 0, 0, 1, $n + EPOCHINEXCEL, 1970);
}


THE CODE TO DISPLAY IT ALL

echo "<p> Today's Date: $priivaDateToday </p>";
echo "<p> Today's Date Unix 2 Excel: ";
echo date('l, M jS', priivaUnix2Excel($priivaDateToday));
echo "</p>";
$priivaDateToday = time();
echo "<p> Today's Date $priivaDateToday </p>";
echo "<p> Today's Date Excel 2 Unix: ";
echo date('l, M jS', priivaExcel2Unix($priivaDateToday));
echo "</p>";

echo "<p> First Recordset: $priivaTestDate </p>";
echo "<p> First recordset, formatted: ";
echo date('l, M jS', priivaExcel2Unix($priivaTestDate));
echo "<br />wtf?</p>";
echo "<hr>";


Now, just to clarify a little bit, this is what's on the rest of the page
(ie, the stuff below the HR that is formatting correctly). I have left out a
few functions and variable definitions as it would archive my database
structure and leave obvious security holes.:

//here we write out the head statement if the date is different
if ($priivaDateSame == 0){
?>
<dt><?php echo date('l, M jS', priivaExcel2Unix($priivaDateAdjusted)); ?>
/ <?php echo $priivaEventDate ?></dt>
<dd><?php echo $priivaEventTitle; ?></dd>

<?php
//If it is the same date, don't repeat the header
}
else if ($priivaDateSame == 1) {
?>
<dd><?php echo $priivaEventTitle; ?></dd>
<?php
//this is the completion of the If statement to check dates
}
//This is a completion of the While loop going through the recordsets
}
while ($RECORDSETSTUFF = mysql_fetch_assoc($RECORDSETSTUFF));
echo "</dl>";
//clears the recordset so it can be re-used.
mysql_free_result($RECORDSETSTUFF);

mysql_free_result($rsDateTest);
?>



--

TIA,

Jon Parkhurst


This topic has been closed for replies.

15 replies

Inspiring
April 27, 2006

> I can't tell you that, simply because I don't know enough details about
> what you're doing right now. ;-D
>
> All I gave were just suggestions ... more or less.


I know. I really needed at leas a month of PHP/SQL study before starting
on this project, but I kinda threw myself into it before I was ready. You'd
think I'd learn sooner rather than later. LOL.

Thanks again mate, I am eternally grateful for all the help and support
you've given me. I hate making posts when I know the info is available with
study, but was bound by time.


Inspiring
April 27, 2006
.oO(crash)

>I'm probably missing what you're saying, but I'm afraid I'm so far into the
>development of this and my knowledge so perilisly close to ignorance that I
>feel I have to keep going on in this way (even if it's not as elegant)
>until I get it working - I don't feel comfortable starting off in a
>completely new direction.

No problem.

>Thanks for all yourhelp, and I'm goign to be referencing your SQL posts
>heavily when I start on the beta of this.

You're welcome.

>If you think I'm off base and should just drop the way "m going, let me
>know.

I can't tell you that, simply because I don't know enough details about
what you're doing right now. ;-D

All I gave were just suggestions ... more or less.

Micha
Inspiring
April 27, 2006
OK, I think I cannot make the calculations in the recordset, b/c I have
events stored that are single entry but use another field to say they repeat
x number of days.

I think it will be easier to convert their serial numbers as a variable, and
then compare that variable to current timestamp - but I can't do this in SQL
b/c I also need to have the other records that have repeating events pulled
as well - unless I make two recordsets, which at this POint I'm just not
willing to do.

I'm probably missing what you're saying, but I'm afraid I'm so far into the
development of this and my knowledge so perilisly close to ignorance that I
feel I have to keep going on in this way (even if it's not as elegant)
until I get it working - I don't feel comfortable starting off in a
completely new direction.

Thanks for all yourhelp, and I'm goign to be referencing your SQL posts
heavily when I start on the beta of this. Right now I just have to get
somethign working, and I think this will work, again just not be as elegant.

If you think I'm off base and should just drop the way "m going, let me
know.


Inspiring
April 25, 2006
That's what I did. I'm having some problems now with an invalid funciton
call. i so *hate* learning while you're tryign to accomplish something, but
I suppose that's my own fault for ignoring PHP for so long....


"Michael Fesser" <netizen@gmx.de> wrote in message
news:ac1t429i38ucnahhdhcu32a71lbj0aprr9@4ax.com...
> .oO(crash)
>
>>Where are you setting these variables? Within the SQL?
>
> Yep, these were SQL variables. But you don't really need them in this
> case.
>
>>I'm getting errors
>>every way I try to set them. I see that you're setting them via command
>>line, correct? How can I set these in my page?
>
> With mysql_query(). But I would probably use PHP variables instead:
>
> $adjst = 1721060;
> $dateFormat = '%W, %b %D';
>
> and then use them in the query string.
>
> Micha


Inspiring
April 25, 2006
.oO(crash)

>Where are you setting these variables? Within the SQL?

Yep, these were SQL variables. But you don't really need them in this
case.

>I'm getting errors
>every way I try to set them. I see that you're setting them via command
>line, correct? How can I set these in my page?

With mysql_query(). But I would probably use PHP variables instead:

$adjst = 1721060;
$dateFormat = '%W, %b %D';

and then use them in the query string.

Micha
Inspiring
April 25, 2006
i think i found it.


Inspiring
April 25, 2006
Micha-

Where are you setting these variables? Within the SQL? I'm getting errors
every way I try to set them. I see that you're setting them via command
line, correct? How can I set these in my page? I'm currenlty reading
through....tryign to find as well as asking.

"Michael Fesser" <netizen@gmx.de> wrote in message
news:kkgs42h48gig597hgmmuinglrc71r91kb6@4ax.com...
> .oO(crash)
>
>>What I am trying to do is capture today's date and compare it with my
>>stored
>>date in my calendar. The stored date in the calendar is a Serial Date + a
>>constant. So I need to capture the time, change it to a Serial date, and
>>then add my constant to compare it to the database.
>
> Wouldn't it be easier to let the database return a properly formatted
> date already instead of fiddling around with this serial nonsense in
> your script?
>
> I already posted something that might help, which involves calling the
> FROM_DAYS() function in MySQL (I just used a wrong constant, so the
> results were off by one year). It can be used to turn the serial day
> count into a proper yyyy-mm-dd date. This can then be used for all kinds
> of date calculations in the DB itself or simply returned as a Unix
> timestamp to your script.
>
> Dealing with "raw" data like a Unix timestamp or an ISO-date is much
> easier than with some proprietary format, which always requires some
> tweaking here and there.
>
> Some tests with your serial dates:
>
> mysql> SET @adjust := 1721060;
> mysql> SET @format := '%W, %b %D';
>
> mysql> SELECT DATE_FORMAT(FROM_DAYS(2453737-@adjust), @format) AS date;
> +-----------------+
> | date |
> +-----------------+
> | Sunday, Jan 1st |
> +-----------------+
>
> mysql> SELECT DATE_FORMAT(FROM_DAYS(2453833-@adjust), @format) AS date;
> +-----------------+
> | date |
> +-----------------+
> | Friday, Apr 7th |
> +-----------------+
>
> mysql> SELECT DATE_FORMAT(FROM_DAYS(2454311-@adjust), @format) AS date;
> +------------------+
> | date |
> +------------------+
> | Sunday, Jul 29th |
> +------------------+
>
> Micha


Inspiring
April 25, 2006
thumps his head on table.

i jus' been outta school too long man. *stuuupid* question. Sorry, too
many issues and not focusing. Thanks Micha.

> Sure, whatever you like (or need):
>
> SELECT
> this,
> that,
> yourDateColumn,
> UNIX_TIMESTAMP(FROM_DAYS(yourDateColumn-1721060)) AS timestamp,
> DATE_FORMAT(FROM_DAYS(yourDateColumn-1721060), '%W, %b %D') AS date,
> somethingMore,
> evenMore
> FROM
> yourTable, anotherTable
> WHERE
> whatever


Inspiring
April 25, 2006
.oO(crash)

>This appears to be a single recordset for just the date. Can I integrate
>this with my current recordsets and still capture the full range of data?

Sure, whatever you like (or need):

SELECT
this,
that,
yourDateColumn,
UNIX_TIMESTAMP(FROM_DAYS(yourDateColumn-1721060)) AS timestamp,
DATE_FORMAT(FROM_DAYS(yourDateColumn-1721060), '%W, %b %D') AS date,
somethingMore,
evenMore
FROM
yourTable, anotherTable
WHERE
whatever

>I might think through this in a moment, but I want to make sure I don't
>misunderstand or go off in the wrong direction for 1/2 a day.

You can add it as an additional field to your query/recordset, so you
can test it together with your current code.

Micha
Inspiring
April 25, 2006
OK, stupid question but I've got my head around the time and don't want to
try to wrap it around SQL.

This appears to be a single recordset for just the date. Can I integrate
this with my current recordsets and still capture the full range of data?

Or do I just need two recordsets, and if so, do I need to do a JOIN?

I might think through this in a moment, but I want to make sure I don't
misunderstand or go off in the wrong direction for 1/2 a day.

Thanks again mate.

"Michael Fesser" <netizen@gmx.de> wrote in message
news:4fms42ps5aq9j2bhlt35skdhdata0uv6qc@4ax.com...
> .oO(crash)
>
>>> Wouldn't it be easier to let the database return a properly formatted
>>> date already instead of fiddling around with this serial nonsense in
>>> your script?
>>
>>Yes! It sure would. Are you subtracting that constant in the @ statements
>>below? It looks like you are with the @adjust.
>
> The @adjust is just a variable I set before sending the query. I could
> also have typed the constant directly in the query. This constant is
> only used to adjust your serial dates to the range that FROM_DAYS()
> uses.
>
>>I'm sorry this is taking me so long to catch on to. I don't know SQL or
>>PHP
>>very well at all and 1/2 of my time is spent tryign to understand the
>>correct notation that I need to put somethign into, and the other 1/2 is
>>spent looking up code fragments to see if they do what I think they do.
>
>
>
>>However, your method looks more efficient if it does what I think it does
>>and might be much better.
>
> Hopefully it also does what _I_ think it does ... :-D
>
> But according to some little tests it works. A full example query
> without any variables for the serial date '2454311':
>
> SELECT DATE_FORMAT(FROM_DAYS(2454311-1721060), '%W, %b %D') AS date;
>
> The following would return a Unix timestamp for the same date:
>
> SELECT UNIX_TIMESTAMP(FROM_DAYS(2454311-1721060)) AS date;
>
> Micha