1
$date =$row2['DeliveryDate'];

$date now contains the date variable as a datetime, to display it I would use:

echo date_format($date, 'm-d-y');

the problem I'm having is extracting single values from $date for example:

$datetime = strtotime($row2['DeliveryDate']);
$mysqldate = date("d", $datetime);

returns this error:

Warning: strtotime() expects parameter 1 to be string, object given in
C:\xampp\htdocs\tutorials\DerBlatt\hebrewDateTrial.php on line 10

I've tried numerous ways to extract the day/month/year into single variables but nothing works if someone can suggest a way it might work I'll be very greatfull; I've copy/pasted code from many sites but all of them use an example of the date as a string, unfortunately i haven't found a solution for the datetime variable.

I wanna do something like:

$date =$row2['DeliveryDate'];
//whatever conversion code that comes in between.
$d = //the day from datetime
$m = //the month from datetime
$y = //the year from datetime
2
  • This is extremely easy to find via a Search Engine (E.G. Google). What have you tried? Commented May 13, 2014 at 23:49
  • strottime(), date('d', $date) and many more ways. Commented May 13, 2014 at 23:50

3 Answers 3

1

If you are using PHP 5.3 or better ,use the DateTime class .

if you want to display in this format $format='m-d-y';

Retrieving data from database .

$date =$row2['DeliveryDate'];
$date = DateTime::createFromFormat('Y-m-d H:i:s',$date);
if($date){ // if the date is correct
    $yourdate = $date->format($format);
    $year = $date->format('Y');
    $month = $date->format('m');
    $day = $date->format('d');
}

Saving to database .

$date = DateTime::createFromFormat($format,$date);    
if($date){
    $date = $date->format('Y-m-d H:i:s');
    $row2['DeliveryDate'] = $date;
}else{
    $row2['DeliveryDate'] = date('Y-m-d H:i:s');
}
Sign up to request clarification or add additional context in comments.

4 Comments

I've found a solution and added it as an answer, but i will try this to see if it works too. Thanks
so i tried it and i get the error: DateTime::createFromFormat() expects at least 2 parameters, 1 given in... as i mentioned in the previous comment i already have a solution and i posted it Thank you
@Abodu Tahiri DateTime::createFromFormat() expects parameter 2 to be string, object given in Thanks for the help i posted the answer already
yes i update the solution , its just another solution .. Good luck bro :)
0

Try this:

echo date('d',strtotime($row2['DeliveryDate'])); 

i think it will work.

4 Comments

nope it doesn't i mentioned in the question that i tried strtotime() but it needs a string, not a variable error: Warning: strtotime() expects parameter 1 to be string, object given in C:\
That doesn't mean the solution is wrong, it means that you're sending an object but not a string. The error is there, throw it in Google and understand the problem.
@effy stackoverflow.com/questions/20073324/… here there is a question about your situation
@Gorkem Yontem thats a different situation i need to convert a datetime object not a string btw i already came up with a solution
0

so this is how i made it work (very funny)...

$m = date_format($row2['DeliveryDate'], 'm');
$d = date_format($row2['DeliveryDate'], 'd');
$y = date_format($row2['DeliveryDate'], 'y');
echo 'Month: '.$m.' Day: '.$d.' Year: '.$y;

Thank you guys for helping me, sometimes my best solution is just using my head...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.