Nico Knoll Posted March 11, 2013 Share Posted March 11, 2013 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 More sharing options...
ryan Posted March 12, 2013 Share Posted March 12, 2013 Is 'timestamp' a field in your system, or one that you are assigning purely for runtime use? Link to comment Share on other sites More sharing options...
Nico Knoll Posted March 12, 2013 Author Share Posted March 12, 2013 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 More sharing options...
SiNNuT Posted March 12, 2013 Share Posted March 12, 2013 I'm not sure but if your $booking->timestamp field is of the type datetime shouldn't you convert the timestamp value to a Y-m-d H:i:s date string before saving? Link to comment Share on other sites More sharing options...
ryan Posted March 14, 2013 Share Posted March 14, 2013 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now