Low End Mac's Online Tech Journal
Modified Date Display and Scheduled Content with PHP
Website Automation with PHP and MySQL, Part 14
Dan Knight - 2002.07.24
The more I use PHP and MySQL, the more I realize how different
the combination is from using a fully integrated database program
like FileMaker
Pro. In FM Pro, you
can define relationships between
fields and have them automatically generated and universally
applied. With MySQL, you have to write a program to manipulate the
data.
Displaying the Date Field
But I'm learning that, and also learning equivalents to old BASIC and FileMaker commands that give me more control over presentation. For instance, MySQL delivers the results of a date field in the format yyyy-mm-dd, so a PHP script would read today's date from a MySQL database as 2002-07-24.
That's fine, but I prefer to user periods instead of hyphens (and some people prefer slashes), and I don't see any need to display the year when all the links are to new content.
In looking through our PHP books, I found the strtr function, which will replace one string with another throughout a field. In this case, I could use strtr to find every hyphen in the date field and replace it with a period. Now today's date would display as 2002.07.24.
That got me halfway there. Now I needed to find an equivalent of the rightstr command so I could extract just the five rightmost characters from the date string. The closest PHP equivalent is the substr command, which can work from either end of a string. substr(string, nn) will keep the first nn characters of a string - but if nn is a negative number, it instead retains the last nn characters.
Here's the code I used to take the MySQL date field, strip it to the last five characters, and replace any hyphen with a period:
$shortdate = $array[pubdate]; $shortdate = substr ($shortdate, -5); $shortdate = strtr ($shortdate, '-', '.'); echo "$shortdate. ";
Which replaces echo "$array[pubdate] "; in my earlier coding.
And I think this looks much nicer.
Scheduled Content
Sometimes writers get material to me days or even weeks in advance. This gives me plenty of time to edit their articles, schedule their release date, and have them almost ready to go on their scheduled publication date. Almost.
That is, everything is ready to go except that I haven't been able to assign a record number until the day an article goes up. But by fiddling around a bit more with PHP, I can now schedule articles days or weeks in advance, assign an ID, add the article to the database, but not have it displayed until the scheduled date and time.
The first part of the process means modifying this line of code:
$latestdate = mysql_fetch_array(mysql_query("SELECT * FROM links ORDER BY timestamp DESC LIMIT 1"));
by adding one thing - ignoring timestamps later than the current time.
First we define the current time:
$rightnow = date(U);
and then we find the latest article earlier than that:
$latestdate = mysql_fetch_array(mysql_query("SELECT * FROM links WHERE timestamp <= $rightnow ORDER BY timestamp DESC LIMIT 1"));
We need to make just one more change: Don't display today's articles with timestamps later than the present:
$get_links = mysql_query("SELECT * FROM links WHERE pubdate = '$latestdate[pubdate]' and timestamp <= $rightnow ORDER BY rank DESC");
Our display script will now determine today's date and the current time, and it won't display links to articles scheduled any later than the present moment.
That's half the battle. Now I need to create a way that lets me schedule the articles by date and time, convert that to a Unix timestamp, and use that in our database instead of the current time that a record is added.
Here's the code I used to test input. Feel free to modify it as you see fit:
- <?php
- $unix_stamp = date(U);
- $cur_year = date(Y);
- $cur_month = date(m);
- $cur_day = date(d);
- $timezone = date(T);
- $now = date(H) . ":" . date(i);
- echo "<form action=$PHP_SELF method=post><input type=hidden name=process value=true><p>
- Publication Date (yyyy.mm.dd): <input type=text name=pubdate size=12 maxlength=10 value=\"$cur_year-$cur_month-$cur_day\"><br>
- Publication Time (no change for now): <input type=text name=pubtime size=6 maxlength=5 value=\"$now\"> $timezone<br>
- <input type=submit name=submit value=submit></p></form>";
- $stamp = $pubdate . " " . $pubtime . ":00";
- $stamp = strtotime ($stamp);
- if ($stamp >= $unix_stamp)
- {$unix_stamp = $stamp ;}
- ?>
We begin by recording the current time as $unix_stamp, then create year, month, day, minute, hour, and timezone data from the timestamp. The year, month, and date are displayed as Publication Date, and the current time is displayed as Publication Time.
If I make no change to this information, my script uses $unix_stamp as the records time of publication, but if I change the date or time of day, it will use the new information unless I set it to earlier than the timestamp. If I do that, it will use the current time.
The strtotime function converts our yyyy-mm-dd and hh:mm data to a Unix timestamp, which is seconds since the epoch (1970.01.01).
With these modifications, I can schedule the next Lite Side article days or weeks in advance. Of course, then there's the whole different issue of Jeff Adkins coming up with something more timely between then and now....
The first article to use the new system is this one, which I
wrote on Tuesday morning just before leaving for work at the camera
store.
Join us on Facebook. Follow us on Twitter.
Recent Online Tech Journal Columns
- Optimized Software Builds Bring Out the Best in Your Mac, 2009.06.30. Applications compiled for your Mac's CPU can load more quickly and run faster than ones compiled for universal use.
- Low End Mac's Safe Sleep FAQ, 2009.06.15. What is Safe Sleep mode? Which Macs support it? How can you enable or disable it? And more.
- The Original Macintosh, 2009.01.12. An in-depth look at the original Macintosh and how it shaped future Macs.
- More in the Online Tech Journal index.
Links for the Day
- Mac of the Day: Macintosh Portable, introduced 1989.09.20. The nearly 16 lb. behemoth was innovative but not a smashing success.
- February 13 in LEM history: 01: Layoffs may hurt Mac market - 02: Unix for the Mac - Rage against the Macintosh - 03: Options to move data from PCs to Macs - 04: Low cost RAM for older 'Books - 06: Apple, IBM, and Intel - 07: Picking the right cheap computer, new or used - 08: I needed to find an older Mac
- Support Low End Mac
Recent Content on Low End Mac
- Fix Home Button Delay, Tablet the Ultimate Mobile PC, iPad Notebook a Possibility, and More, iOS News Review, 2012.02.10. Also using your iPad at work, two photo editors, a new iPad text editor, Macally's magnetic iPad 2 stand, and more.
- White MacBook Goes End-of-Life, Logitech Touch Mouse Supports Gestures, Firmware Updates, and More, The 'Book Review, 2012.02.10. Also MacBook Air better than any Ultrabook, docks for MacBook Pro models, Intel offers improved SSDs, and more.
- Mac and iOS Browsers: Options Galore, Freeware Forum, 2012.02.10. Safari is adequate on Mac and great on iOS, but the range of good alternatives is stunning. LEM writers share their favorites.
- Apple's Support Lead Shipping, Smartphones Outsell PCs, OS X Ported to ARM by Intern, and More, Mac News Review, 2012.02.10. Also the power of Tex-Edit Plus, Google and Twitter are already censoring the Web, Snow Leopard Security Update, and more.
- LogMeIn: Remote Screen Sharing for the Rest of Us, Alan Zisman, Zis Mac, 2012.02.09. Configuring the Mac's built-in screen sharing to work over the Internet can be difficult or impossible. LogMeIn makes it easy.
- 15 Years Ago Motorola Unveiled the PowerPC G3, Low End Mac Round Table, 2012.02.06. The G3 processor was optimized for real world Mac software and made a big leap forward in efficiency.
- Don't Kill Caps Lock, Learning to Love the iOS Keyboard, and an Adaptive iPad Keyboard, Charles W. Moore, Miscellaneous Ramblings, 2012.02.06. The Caps Lock key has a useful function, the iPad's keyboard really is useful, and checking out an adaptive keyboard for the iPad.
- More links in our archive.
Recent Deals
- Best MacBook Air Deals
- Best iBook G4 Deals
- Best iPad Deals
- Best Classic Mac OS Deals
- Best Apple TV Deals
- Best 15" MacBook Pro Deals
- Best Power Mac G4 Deals
- Best Mac OS X 10.6 Deals
- More deals in our archive.
About LEM Support Usage Privacy Contact
Follow
Low End Mac on Twitter
Join Low End Mac
on Facebook
Low End Mac Reader Specials
TypeStyler 11 is now in the Mac App Store!! -- Special Introductory Price of $59.95!! -- To Buy From The Mac App Store Click Here Now!! Or buy direct
from Strider Software.
Don't install Parallels to play poker online! Poker Mac will show you how to download and install a native Mac poker and Mac Casino applications in minutes.
Favorite Sites
MacSurfer
Cult of Mac
Shrine of Apple
MacInTouch
MyAppleMenu
InfoMac
The Mac Observer
Accelerate Your Mac
RetroMacCast
PB Central
MacWindows
The Vintage Mac Museum
DealMac
Deal Brothers
Mac2Sell
Mac Driver Museum
JAG's House
System 6 Heaven
System 7 Today
the pickle's Low-End Mac FAQ
Affiliates
Amazon.com
The Apple Store
The iTunes Store
PC Connection Express
GainSaver
Parallels Desktop for Mac
eBay

