Jump to content

Add Time & Date Field via API


Nico Knoll
 Share

Recommended Posts

Hey,

I tried something like this code:

$page->timestamp = 123456789; // timestamp is a date & time field
$page->save();

But it won't add the timestamp... The field stays empty...

Do I have to format the day before or something like this?

-- Nico

Link to comment
Share on other sites

It's a real field. The whole code looks like this:

<?php // change the line above to reflect your system path or remove if not command-line

set_time_limit(10000);

include("../index.php"); // bootstrap ProcessWire's API, if applicable

// open the CSV file with the data in it
$fp = fopen('booked.csv', 'r'); 
$n = 0;

$labels = array();

echo '<pre>';

while(($data = fgetcsv($fp, 0, ';', '"', '\\\\')) !== false) {

    if(++$n == 1) {
        // if we're on the first line, save the CSV labels and skip over the line
        $labels = $data; 
        continue; 
    }

    // use the labels we found from line 1 to make the $data array associative
    foreach($data as $key => $value) {
        $label = $labels[$key]; 
        $data[$label] = $value; // now $data[1] is $data[pid], for example
        unset($data[$key]); // don't need the numbered index anymore
    }
    
    foreach($data as $key => $value) {
        if($key == 'data') {
            $data['data'] = unserialize(html_entity_decode($value));
        }
    }

    $times = explode(':', $data['time']);
    $time = ((int)$times[0] * 60 * 60) + ((int)$times[1] * 60);
    $timestamp = $time + $data['date'];
    
    $client = array(
        'prename' => $data['data']['prename'],
        'subname' => $data['data']['subname'],
        'tel'     => $data['data']['tel'],
        'email'   => $data['data']['email']
    );

    var_dump($timestamp, $client);
    saveBooking($timestamp, $client);

}

function saveBooking($timestamp, $client = '') {
    $booking = new Page();
    $booking->template = wire('templates')->get('booking');
    $booking->parent = wire('pages')->get('/bookings/');

    $booking->title = 'booking-'.time();
    $booking->timestamp     = $timestamp;
    $booking->client_prename = $client['prename'];
    $booking->client_subname = $client['subname'];
    $booking->client_tel     = $client['tel'];
    $booking->client_email   = $client['email'];

    $booking->save();    
}

echo '</pre>';
?>
Link to comment
Share on other sites

It looks okay to me. I would double check that $timestamp is an integer (which PW wants if you set it as a literal unix timestamp), so you may want to typecast it: $timestamp = (int) $timestamp; PW will happy take a string or integer, but if it's a string, it assumes it has to run it through strtotime() first. Whereas if you give it an integer, it knows it's a unix timestamp and doesn't attempt to translate it. 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...