Low End Mac Reader Specials
TypeStyler For Mac OS X is Now Shipping! Download The Free Fully Functional 60 Day Tryout at www.typestyler.com
Don't install Parallels to play poker online! Poker Mac will show you how
to download and install a native Mac poker application such as Full
Tilt Poker Mac.
Compare products like desktop computers, apple laptops, apple macs, and LCD Monitors side by side! All the information and reviews to make the best purchasing decision for new mobile phones, sat nav systems, or MP3 players. The Ciao online shopping community makes searching products easy for you.
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.
Recent Online Tech Journal Columns
- Optimized Software Builds Bring Out the Best in Your Mac, 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, 06.15. What is Safe Sleep mode? Which Macs support it? How can you enable or disable it? And more.
- The Original Macintosh, 01.12. An in-depth look at the original Macintosh and how it shaped future Macs.
- The Innovative Lisa, 01.08. Apple's Lisa and how it paved the way for the Macintosh.
- More in the Online Tech Journal index.
Links for the Day
- Mac of the Day: Power Mac G5/2.7 GHz, Apr. 2005 - The fastest G5 CPU Apple ever used, this model included a 16x SuperDrive and shipped with 'Tiger' (OS X 10.4).
- Group of the Day: MacBook List for those using a MacBook or MacBook Pro.
- March 12 in LEM history: 01: When should I replace my Mac? - 02: Who stole my domain? - Happiness is a new Power Mac - 03: USB for wireless networking - 04: PowerBooks have some room for improvement - 07: 11 ways to optimize Mac performance - Finding older software for Macs - Booting PowerPC Macs from USB - Installing Leopard from an Intel Mac
- Support Low End Mac
Recent Content on Low End Mac
- Consumer Reports Rates Apple Tops, Macs Cost Less to Manage than PCs, 6 Core Mac Pro Soon?, and More, Mac News Review, 03.12. Also dreaming of a Mac mini on steroids, focus on word processiong, Ubuntu ditches brown for more Mac-like appearance, and more.
- OS X 10.4 Tiger Still Very Usable on a 500 MHz G3 Mac, Simon Royal, Mac Spectrum, 03.11. For writing and basic Internet access, a 500 MHz G3 provides sufficient power and Tiger provides fairly up-to-date software.
- WPA for Original AirPort, Stainless Browser, Multiple Input Bug Persists in Snow Leopard, and More, Charles W. Moore, Miscellaneous Ramblings, 03.11. Also kudos for Shiira, G3 vs. G4 upgrade for Pismo PowerBook, and 17" PowerBook still suffices.
- iPad Gaming Potential, Dan Bashur, Apple, Tech, and Gaming, 03.11. Two years of developing games for the less powerful iPhone and iPod touch has prepared developers to unleash the iPad's potential.
- Apple Now Sees Microsoft as Less Evil than Google, Frank Fox, Stop the Noiz, 03.10. With Android threatening the iPhone's dominance, Apple views Microsoft and Windows 7 Mobile as the lesser threat.
- More links in our archive.
Recent Deals
- Best iPod classic Deals, 03.12. Used 20 GB, $119; 40 GB, $139; 60 GB, $159; 30 GB video, $129; 60 GB, $159; 80 GB, $169; refurb 120 GB, $189; new, $214; 160 GB, $228 shipped.
- Best G3 iBook and AirPort Card Deals, 03.12. 366 MHz 12" clamshell, $89; 466, $125; 500 white CD, $100; 600, $199; 800 Combo, $239; 14" 900, $225.
- Best Xserve Deals, 03.12. Used 1 GHz dual G4, $499; 2.0 dual G5, $599; 2.3, $749; refurb 2.26 4-core Nehalem, $2,499; new, $2,699; 8-core, $3,449; refurb 2.66, $4,299; new, $4,799; more.
- Best iPod touch Deals, 03.11. Refurb 8 GB, $149; 16 GB, $199; 32 GB, $249; 64 GB, $339; new 3G/8 GB, $184; close-out 2G/16 GB, $229; 3G/32, $270; 64, $355. Shipping included.
- Best Mac OS X 10.5 Deals, 03.11. "Leopard" one user, $180; upgrade from 10.4, $150; 5 users, $400; Server, 10 users, $493; unlimited users, $600.
- Best 17" MacBook Pro Deals, 03.11. Used 2.33 GHz, $1,099; 2.5, $1,349; refurb 2.66, $1,949; 2.93, $2,199; new 2.8, $2,249 after rebate; 3.06, $2,749.
- Best iPod nano Deals, 03.10. Refurb 4G/8 GB, $99; 16 GB, $119; 4G/8 GB, $129; 16 GB, $139; new 5G/8 GB, $134; 16 GB, $160. Shipping included.
- Best 15" PowerBook G4 Deals, 03.10. 1 GHz Combo, $400; 1.25 GHz, $460; 1.33 GHz SuperDrive, $539; 1.5 GHz, $550; 1.67 GHz, $589; hi-res, $800.
- Best iPod shuffle Deals, 03.10. New 3G/2 GB, pink, $53; other, $55, 4 GB, blue, $71; other, $73. Shipping included.
- More deals in our archive.
About LEM | Support | Usage | Privacy | Contacts
Navigation
Used Mac Dealers
Apple History
Video Cards
Email Lists
Favorite Sites
MacSurfer
MacMinute
MacInTouch
MyAppleMenu
InfoMac
Macs Only!
The Mac Observer
Accelerate Your Mac
RetroMacCast
PB Central
MacWindows
The Vintage Mac
Museum
DealMac
DealsOnTheWeb
Mac2Sell
ramseeker
Mac Driver Museum
JAG's House
System
6 Heaven
System 7 Today
the pickle's Low-End
Mac FAQ
Abandonware
Petition
Mac vs. PC Info
Affiliates
The Apple
Store
Mac
Connection
B&H
MacMall
TechRestore
ExperCom
Crucial
Memory
batteries.com
MacMinute
MacInTouch
MyAppleMenu
InfoMac
Macs Only!
The Mac Observer
Accelerate Your Mac
RetroMacCast
PB Central
MacWindows
The Vintage Mac
Museum
DealMac
DealsOnTheWeb
Mac2Sell
ramseeker
Mac Driver Museum
JAG's House
System 6 Heaven
System 7 Today
the pickle's Low-End
Mac FAQ
Abandonware
Petition
Mac vs. PC Info
Mac Connection
B&H
MacMall
TechRestore
ExperCom
Crucial Memory
batteries.com
