PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old October 22nd, 2009, 10:14 AM
HHawk HHawk is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 55 HHawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 40 m 49 sec
Reputation Power: 2
php5 - Quick question about strftime.

How do I display the following:

Current date - 1 day (yesterday's date)
Current date - 2 days (the date of the day before yesterday)
Current date - 3 days (the date of 3 days ago)
Current date - 4 days (the date of 4 days ago)

I already went through the PHP.net manual, but I do not understand how to achieve this.

I just want to display the dates from 1, 2, 3 or 4 days ago on my website...

Reply With Quote
  #2  
Old October 22nd, 2009, 12:12 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
strtotime() is probably the easiest. takes a string and tries to calculate in a timestamp what the string is saying.

PHP Code:
//this will get you a timestamp for each day.
$today time();
$day1 strtotime("-1 day");
$day2 strtotime("-2 days");
$day3 strtotime("-3 days");
$day4 strtotime("-4 days");

//using the timestamp from above you can use date() to get out a date

echo date("F jS, Y",$day1).'<br />';
echo 
date("F jS, Y",$day2).'<br />';
echo 
date("F jS, Y",$day3).'<br />';
echo 
date("F jS, Y",$day4); 
Comments on this post
HHawk agrees: Great, exactly what I was looking for!

Reply With Quote
  #3  
Old October 23rd, 2009, 03:39 AM
HHawk HHawk is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 55 HHawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 40 m 49 sec
Reputation Power: 2
Thumbs up

Great, exactly what I was looking for.
Why does it always look so easy to pull of?!

Thnx once again!

//edit


One problem, now my months are in English and I want them to be in Dutch? That's why I was using strftime (nl_NL).

How can I display the months in Dutch with your example?

Last edited by HHawk : October 23rd, 2009 at 04:00 AM.

Reply With Quote
  #4  
Old October 23rd, 2009, 01:21 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
strftime has an opposite function strptime that takes a string formatted with strftime and the format string used in strftime and converts it to an array with all the time parts (day, month, year, hour...etc) which can be easily converted back to a timestamp with mktime(). However, strptime() is not implemented on windows yet so you would have to be using a *nix based OS. On windows, you would have to make your own function to get the date back. You can check out some of the user notes on the strptime manual page and there might be something that could work. Other than that, you can also do a simple string replace on the string to change the months to english months before using strtotime().

Reply With Quote
  #5  
Old October 26th, 2009, 04:07 AM
HHawk HHawk is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 55 HHawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 40 m 49 sec
Reputation Power: 2
Quote:
Originally Posted by IAmALlama
strftime has an opposite function strptime that takes a string formatted with strftime and the format string used in strftime and converts it to an array with all the time parts (day, month, year, hour...etc) which can be easily converted back to a timestamp with mktime(). However, strptime() is not implemented on windows yet so you would have to be using a *nix based OS. On windows, you would have to make your own function to get the date back. You can check out some of the user notes on the strptime manual page and there might be something that could work. Other than that, you can also do a simple string replace on the string to change the months to english months before using strtotime().


I am using a Plesk with *nix OS.

Reply With Quote
  #6  
Old October 26th, 2009, 12:27 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,937 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 5 Days 1 h 54 m 18 sec
Reputation Power: 4
well then you should be able to use strptime() with no problems. something like:
PHP Code:
//the format for use in strftime
$format "%B %e, %G";

//get strftime
$fTime strftime($format);

//display the before
echo "$fTime<hr>";

//convert to an array with the date parts.
$pTime strptime($fTime$format);

//display the array
echo "<pre>".print_r($pTimetrue)."</pre><hr>";

//convert the date parts back to a timestamp
$timeStamp mktime($pTime['tm_hour'],$pTime['tm_min'],$pTime['tm_sec'],$pTime['tm_mon'],$pTime['tm_mday'],$pTime['tm_year']);

//display the timestamp
echo "$timeStamp <hr>";

//now you can do all the strtotime conversions you want
$newTime strtotime("-5 days"$timeStamp);

//make a new date format from strftime using the new timestamp
$newfTime strftime($format$newTime);

//display the new time
echo $newfTime

I don't have a *nix station at work to test, but that should work. You could probably wrap most of that in a function where you pass the format, time string from the original strftime and the string input for strtotime (ie +1 day) and it returns the modified new strftime string.

Reply With Quote
  #7  
Old October 26th, 2009, 04:31 PM
HHawk HHawk is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 55 HHawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 40 m 49 sec
Reputation Power: 2
Quote:
Originally Posted by IAmALlama
well then you should be able to use strptime() with no problems. something like:
PHP Code:
//the format for use in strftime
$format "%B %e, %G";

//get strftime
$fTime strftime($format);

//display the before
echo "$fTime<hr>";

//convert to an array with the date parts.
$pTime strptime($fTime$format);

//display the array
echo "<pre>".print_r($pTimetrue)."</pre><hr>";

//convert the date parts back to a timestamp
$timeStamp mktime($pTime['tm_hour'],$pTime['tm_min'],$pTime['tm_sec'],$pTime['tm_mon'],$pTime['tm_mday'],$pTime['tm_year']);

//display the timestamp
echo "$timeStamp <hr>";

//now you can do all the strtotime conversions you want
$newTime strtotime("-5 days"$timeStamp);

//make a new date format from strftime using the new timestamp
$newfTime strftime($format$newTime);

//display the new time
echo $newfTime

I don't have a *nix station at work to test, but that should work. You could probably wrap most of that in a function where you pass the format, time string from the original strftime and the string input for strtotime (ie +1 day) and it returns the modified new strftime string.


Thnx man, I will give that a go.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > php5 - Quick question about strftime.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek