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, 09:14 AM
HHawk HHawk is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 59 HHawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 6 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, 11:12 AM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 2,070 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 6 Days 8 h 15 m 37 sec
Reputation Power: 5
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, 02:39 AM
HHawk HHawk is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 59 HHawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 6 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 03:00 AM.

Reply With Quote
  #4  
Old October 23rd, 2009, 12:21 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 2,070 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 6 Days 8 h 15 m 37 sec
Reputation Power: 5
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, 03:07 AM
HHawk HHawk is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 59 HHawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 6 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, 11:27 AM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 2,070 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 6 Days 8 h 15 m 37 sec
Reputation Power: 5
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, 03:31 PM
HHawk HHawk is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 59 HHawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 6 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!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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




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