Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.




1259 posts

Uber Geek
+1 received by user: 147

Subscriber

# 175307 24-Jun-2015 17:15
Send private message

I'm having a play with MYSQL, PHP and WordPress but am stuck on something I can't figure out. I'm not a PHP developer but have done plenty of plsql in the past which may or may not be helping !

I have two tables. table_1 is a 'reference' and has a date field. When a record is inserted into table_2 I would like to populate the column table_2.date dependant on the value in table_1.date. The logic is if table_1.date is > today I would like to return 'last tuesday' from that future date otherwise I would like to return 'next tuesday' from today.

I've learnt if I create dates I can do the following $date->modify('last tuesday') and I've learnt if I want to test dates against themselves I need to convert the fields to timestamps first.

So far I have the following (the echo's where just trying to help me see what I had where, rather than having inserts) :

[code]
global $wpdb;
$querystr ="select date from table_1 where id=blah";
$myrows = $wpdb->get_results($querystr, ARRAY_A);

  if ($myrows) {
    foreach ( $myrows as $myrow ) {
      // what do I have from the table ?
      echo 'date: ' . $myrow['date'] . '<br/>';

      // setup the variables I need
      $today_end = strtotime('tomorrow');
      $date_timestamp=strtotime($myrow['date']);

      if ($date_timestamp >= $today_end) {
        // return last tuesday from that date
        $new_date->modify('last tuesday');
        echo 'date last tuesday : ' . date_format($date, 'Y-m-d') .;
      } else {
        // Return next tuesday from today
        $new_date->modify('next tuesday');
        echo 'date next tuesday : ' . date_format($date, 'Y-m-d') .;
     }
  }
}
else {
  ?><h2Nothing Found</h2><?php
}
[/code]

Where I believe I'm going wrong is in with $new_date->modify('blah'); If $new_date is a created with $new_date=date_create('2015-06-24'); for example the ->modify works perfectly, but I want to use the variable returned from $myrow['date']

I obviously have some sort of datatype mismatch going on or have mixed two incompatible methods. Can anyone point me in the right direction ?

Thanks



Filter this topic showing only the reply marked as answer Create new topic
279 posts

Ultimate Geek
+1 received by user: 39


  # 1330895 24-Jun-2015 17:56
One person supports this post
Send private message

Initialize $new_date with the date from the row before you modify it 

1669 posts

Uber Geek
+1 received by user: 283

Subscriber

  # 1330905 24-Jun-2015 18:07
Send private message

Yep what Xeon said.  You are using a mixture of the dateTime class and strtotime function for clarity but if you want to use the strtotime as well->

Something along the lines of

$new_date = new DateTime($myrow['date']);  Then modify this.  This is assuming the date format from the DB is YYYY-MM-DD

 
 
 
 




1259 posts

Uber Geek
+1 received by user: 147

Subscriber

  # 1330966 24-Jun-2015 20:46
Send private message

I knew it was the initialisation I was missing but I couldn't figure out the exact syntax !

Thanks itxtme that worked a treat :)

Filter this topic showing only the reply marked as answer Create new topic



Twitter and LinkedIn »



Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:





News »

Xero announces new smarter tools, push into the North American market
Posted 19-Jun-2019 17:20


New report by Unisys shows New Zealanders want action by social platform companies and police to monitor social media sites
Posted 19-Jun-2019 17:09


ASB adds Google Pay option to contactless payments
Posted 19-Jun-2019 17:05


New Zealand PC Market declines on the back of high channel inventory, IDC reports
Posted 18-Jun-2019 17:35


Air New Zealand uses drones to inspect aircraft
Posted 17-Jun-2019 15:39


TCL Electronics launches its first-ever 8K TV
Posted 17-Jun-2019 15:18


E-scooter share scheme launches in Wellington
Posted 17-Jun-2019 12:34


Anyone can broadcast with Kordia Pop Up TV
Posted 13-Jun-2019 10:51


Volvo and Uber present production vehicle ready for self-driving
Posted 13-Jun-2019 10:47


100,000 customers connected to fibre broadband network through Enable
Posted 13-Jun-2019 10:35


5G uptake even faster than expected
Posted 12-Jun-2019 10:01


Xbox showcases 60 anticipated games
Posted 10-Jun-2019 20:24


Trend Micro Turns Public Hotspots into Secure Networks with WiFi Protection for Mobile Devices
Posted 5-Jun-2019 13:24


Bold UK spinoff for beauty software company Flossie
Posted 2-Jun-2019 14:10


Amazon Introduces Echo Show 5
Posted 1-Jun-2019 15:32



Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.


Support Geekzone »

Our community of supporters help make Geekzone possible. Click the button below to join them.

Support Geezone on PressPatron



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.