View Full Version : Admin Date
03-11-2007, 09:35 AM
So I've been working on my admin section lately, before I start working on the main design. Now I've got a problem I'm not quite sure how to work out. I've got date and time settings in my admin page, which I can update, and the settings in the database will be updated. So if my date is for example:
d M Y
I can call it from the database and post it as a date. Problem is, when I want to insert a newsitem, the date will also be posted. But I do not wish to actually post the date into the database, I would rather actually wish for the date format to change whenever I change this in my admin section. I don't think it's possible though, but I'm asking it anyway.
I made it work with my private messages for example. Whenever someone changes their display name, that name will be shown in the private messages list, also the ones that have already been sent. I only wish for the date and time to do the same, otherwise my date and time admin section are of no use.
03-11-2007, 11:21 AM
Wait, let me get this straight, you want to make it so each of your news items shares the same dates and times? Or do you mean like date and time offsets for different timezones or daylight savings areas?
To answer whatever your question will be, yes this can be done and quite easily at that. To start with, I'd recommend you start using timestamps with your date values instead (especially if you are looking at the timezone usage) as this will greatly simplify your work. Get back as to what exactly you mean for the implementation of the time.
Man I hate being tired. I gotcha now, you simply want to create a format for your time correct? Simple.
First step, make sure you are using timestamps, this will make it easy. Use them for everything, from dates to times.
Second step, when calling anything that requires the formatting, you need to query what the format is. I'd recommend somehow storing this with your common data in an array or object of common functionatlity.
Third. Use the date() (http://ca3.php.net/manual/en/function.date.php) in combination with your timestamp:
$postDate = date($common->dateFormat, $postTimeStamp);
Does this answer your question?
03-11-2007, 02:49 PM
Thanks a lot,
I read it, and I think I will get this to work, with whatever I want/need. I have to say though, that I won't be checking it anytime soon, since I'm working on different things at the moment. But I'll be sure to have checked it by tonight, and see if it does what I want. Whatever happens, I'll post here again later today.
I believe I also read it wrong :P I wish for the date format to change (for posts which have already been made aswell) when I change the value in the admin section...
03-11-2007, 09:00 PM
Ok, you simply need to use a consistant data source for everything. Since I have recommended that you use a timestamp to store your data, you will need to update the posts as well.
Best function for that would be strtotime() (http://ca3.php.net/manual/en/function.strtotime.php). This will allow you to pass your formatted date in and return a timestamp.
Now, this could give you a problem, it has specified guidlines to follow, and unfortunatly I'm not 100% certain as to how it operates when it parses. However, you can simply check this by running a script to compare the output before you look at updating:
// I will assume that the value $newsDate has been queried already
$newDate = strtotime($newsDate);
// d M Y should be replaced with the format used by your $newsDate
$newDateFormatted = date('d M Y', $newDate);
// ugh, I'm used to java so without a compareto I will attempt an exact equals instead:
if ($newDateFormatted === $newsDate)
echo "The date is correctly formatted\n";
echo "The date is not correctly formatted\n";
The formatting should match your original, all this script does is take the current, attempt to parse the format into a timestamp, format the new timestamp back into its original format and compare them. As my comment mentions, I'm not 100% certain if the === operator will work with strings, so you may want to simply output the original format with the new format to visually compare them instead. As well, depending on your php version, $newDateFormatted could be a null value, which will throw you some errors.
If it appears to work correctly, you can use the same concept to write the timestamp and update your database.
Let me know if you've got any troubles.
03-13-2007, 10:51 PM
You will need to update the posts as well.
But that's exactly what I wanted to know, if it is possible without updating the existing posts? This will require a lot of coding which I do not wish to do. I simply want to alter the date format in the database, and if I do so, the date format for every existing post will be altered aswell. I though it wasn't possible, and I think you will be telling me the same next :P
Many thanks for your help so far, it's greatly appreciated!
03-13-2007, 11:52 PM
Read this article (http://www.guelphdad.wefixtech.co.uk/sqlhelp/varchartodate.shtml) by Guelphdad on how to convert a database field to date or datetime type. It is well worth the effort to do so.
After you do that you may need to make adjustments to how PHP treats the data coming from that field, but again, well worth the effort looking into the future.