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 ??

LVL 23
savoneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
savoneAuthor Commented:
Can you give me a little explanation?

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

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.