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.


martyyn

1971 posts

Uber Geek

ID Verified
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



Create new topic

This is a filtered page: currently showing replies marked as answers. Click here to see full discussion.

itxtme
2102 posts

Uber Geek


  #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

Create new topic





News and reviews »

Air New Zealand Starts AI adoption with OpenAI
Posted 24-Jul-2025 16:00


eero Pro 7 Review
Posted 23-Jul-2025 12:07


BeeStation Plus Review
Posted 21-Jul-2025 14:21


eero Unveils New Wi-Fi 7 Products in New Zealand
Posted 21-Jul-2025 00:01


WiZ Introduces HDMI Sync Box and other Light Devices
Posted 20-Jul-2025 17:32


RedShield Enhances DDoS and Bot Attack Protection
Posted 20-Jul-2025 17:26


Seagate Ships 30TB Drives
Posted 17-Jul-2025 11:24


Oclean AirPump A10 Water Flosser Review
Posted 13-Jul-2025 11:05


Samsung Galaxy Z Fold7: Raising the Bar for Smartphones
Posted 10-Jul-2025 02:01


Samsung Galaxy Z Flip7 Brings New Edge-To-Edge FlexWindow
Posted 10-Jul-2025 02:01


Epson Launches New AM-C550Z WorkForce Enterprise printer
Posted 9-Jul-2025 18:22


Samsung Releases Smart Monitor M9
Posted 9-Jul-2025 17:46


Nearly Half of Older Kiwis Still Write their Passwords on Paper
Posted 9-Jul-2025 08:42


D-Link 4G+ Cat6 Wi-Fi 6 DWR-933M Mobile Hotspot Review
Posted 1-Jul-2025 11:34


Oppo A5 Series Launches With New Levels of Durability
Posted 30-Jun-2025 10:15



Geekzone Live »

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



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.