[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

PHP printf output into a varibale

I am attempting to learn php.  Can someone explain to me why this is not working?


        $dayofweek = date( "w" );
        $day = date("d");
        $thisweek = $day - $dayofweek;
        $lastweek1 = $thisweek - 7;
        $lastweek = `printf("0%d", $lastweek1)`;

echo "$lastweek";

The lastweek variable is empty, how can I get the output of printf into the variable $lastweek ??

0
savone
Asked:
savone
  • 5
  • 3
  • 2
  • +3
2 Solutions
 
DVation191Commented:
Try using sprintf instead of printf
http://php.net/manual/en/function.sprintf.php
0
 
CKY092Commented:
The reason you're having trouble is you're trying to set the variable $lastweek to printf(), which is a function to print to the screen.

If I follow your variable names properly, you're looking to get the date of last week. Here's a simple way to do that:
$lastweek = mktime(0, 0, 0, date("m"), date("d")-7,   date("Y"));
echo date('F d, Y',$lastweek);

Open in new window

And you can change to format string in the date() function to display the output however you'd like.

Hope that helps.
0
 
savoneAuthor Commented:
Can you give me a little explanation?

All the examples on that link use printf....

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
savoneAuthor Commented:
@CKY092

That great information, but it returns 7 days behind today, which is not exactly what I was going for.  I need to know the Sunday of last week. So I am trying:


        $dayofweek = date( "w" );                        // Numeric representation of day of week 6 for sat 0 for sun
        $day = date("d");                                        // day of month
        $thisweek = $day - $dayofweek;            // day of month minus day of week brings me to the last sunday
        $lastweek1 = $thisweek - 7;                   // this brings me to the sunday before last
        $lastweek = `printf("0%d", $lastweek1)`;

0
 
savoneAuthor Commented:
I made it work like this:

       $dayofweek = date( "w" );                        // Numeric representation of day of week 6 for sat 0 for sun
        $lastweek2 = mktime(0, 0, 0, date("m"), date("d")-$dayofweek-7, date("Y"));
        $lastweek = date('Y-m-d', $lastweek2);


How ugly is this?
0
 
CKY092Commented:
So do you want the last Sunday or the Sunday of the preceding week?

I don't think it's that ugly. ;)
0
 
savoneAuthor Commented:
Well this sunday would be this week, the sunday before last would be last week.

Make sense?
0
 
DVation191Commented:
The reason printf isn't working is becausesprintf prints to the screen where sprintf returns a string - something you can store to a variable and echo later.
0
 
CKY092Commented:
I suppose this is a matter of week starting and week ending days. The long and the short of it is, as long as you're getting the result you want, we've accomplished the goal.
0
 
Beverley PortlockCommented:
"Can you give me a little explanation?

All the examples on that link use printf...."



sprintf and printf are exactly the same except that printf sends output to the screen and sprintf sends it to a string. Anything that works for printf will work for sprintf as well.

You need to be looking at strtotime for manipulating dates, for example...

<?php

$lastSunday =  strtotime("LAST SUNDAY");
$sundayBefore = strtotime("LAST SUNDAY -7 DAYS");

echo "Last sunday was " . date( "Y-m-d", $lastSunday) . " and 7 days before it was " . date("Y-m-d", $sundayBefore );

Open in new window


which gives

Last sunday was 2011-10-09 and 7 days before it was 2011-10-02
0
 
Beverley PortlockCommented:
I should have added

http://uk.php.net/manual/en/datetime.formats.php  for the various formats strtotime converts and in particular this page http://uk.php.net/manual/en/datetime.formats.relative.php which lists the relative text formats
0
 
Ray PaseurCommented:
You might want to take a look at this article to learn some ways to process DATETIME information in PHP and MySQL.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Also, all of the PHP functions are documented in the online man pages, so if you are not 100% sure what a function requires for input or returns for output, you can look it up.  Example:
http://php.net/manual/en/function.printf.php
http://php.net/manual/en/function.sprintf.php

And if you haven't got it yet, get this excellent little book from SitePoint.  Very readable with great examples, and a downloadable code library that you can copy and modify for your own use.  Now in its fourth printing, it has been a permanent part of my professional library since Edition One.
http://www.sitepoint.com/books/phpmysql4/

Best of luck with PHP, ~Ray
0
 
Sandeep KothariProject LeadCommented:
see I cant get what are you doing ....
date('d') - date('w') ?? ? read this first .. . .

http://php.net/manual/en/function.date.php
0
 
Ray PaseurCommented:
See: http://www.laprbass.com/RAY_temp_savone.php
<?php // RAY_temp_savone.php
error_reporting(E_ALL);
echo "<pre>"; // READABILITY


/* FROM THE POST AT EE
 * Well this sunday would be this week, the sunday before last would be last week.
 * Make sense?
 */

// REQUIRED PHP 5.1+
date_default_timezone_set('America/Chicago');

// A POINT OF REFERENCE: NOW - INCLUDING THE TIME OF DAY
var_dump( date('r') );

// SHOW SOME WAYS OF GETTING TO DIFFERENT SUNDAY DATES
var_dump( date('r', strtotime('Sunday')) );
var_dump( date('r', strtotime('Last Sunday')) );
var_dump( date('r', strtotime('Last Sunday - 7 days')) );
var_dump( date('r', strtotime('Sunday - 2 weeks')) );
var_dump( date('r', strtotime('Next Sunday')) );

// HOWEVER THIS FAILS - NOTE THE OUTPUT
var_dump( date('r', strtotime('First Sunday in 2012')) );

Open in new window

0
 
savoneAuthor Commented:
Thanks for the info
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now