I’m trying to get the time and date in RFC3339 format (Y-m-d\TH:i:sP) but whenever I try it just returns NULL. I’ve got a form that somebody fills out, when submitted it’s supposed to send the results using REST API
This form is:
$formid=0;
$FORM[$formid]['target']="/sms.php";
$FORM[$formid]['method']="post";
$FORM[$formid]['submit']="Add SMS";
$FORM[$formid]['commit']=1;
$FORM[$formid]['width']=300;
$FORM[$formid]['field']['message']['label']="Message";
$FORM[$formid]['field']['message']['type']="text";
$FORM[$formid]['field']['message']['autofocus']=true;
$FORM[$formid]['field']['message']['err']['notempty']=true;
$FORM[$formid]['field']['date']['label']="Date";
$FORM[$formid]['field']['date']['type']="date";
$FORM[$formid]['field']['date']['elements']=array( "day","month","year" );
$FORM[$formid]['field']['date']['value']=mktime();
$FORM[$formid]['field']['time']['label']="Time (24hr)";
$FORM[$formid]['field']['time']['type']="time";
$FORM[$formid]['field']['time']['elements']=array( "hour","minute","second" );
$FORM[$formid]['field']['time']['value']=time();
After submitting the form this is what happens:
$date=mktime( 2,0,0,$_POST['month'],$_POST['day'],$_POST['year'] );
$time=time( 2,0,0,$_POST['hour'],$_POST['minute'] );
require_once('autoload.php');
$MessageBird = new \MessageBird\Client('ACCESS_KEY');
$Message = new \MessageBird\Objects\Message();
$Message->originator = 'MessageBird';
$Message->recipients = array(447551159632);
$Message->scheduledDatetime = date_timestamp_set($date,RFC3339);
$Message->body = $_POST['message']." Opt out: STOP to 8888";
$MessageBird->messages->create($Message);
try {
$MessageResult = $MessageBird->messages->create($Message);
var_dump($MessageResult);
The results from the var_dump are:
date_timestamp_set is an alias of DateTime::setTimestamp, which in procedural style takes two parameters: a DateTime Object as Parameter 0, and an Integer as Parameter 1. You are feeding it an Integer as Parameter 0, and a non-declared literal (which will get assumed as String) as parameter 1.
Sorry to be dumb but I don’t understand what you mean? Is it not possible to do it the way I’ve done it?
http://us3.php.net/manual/en/datetime.settimestamp.php
You’re giving it the wrong parameters.
If what you want is a string representation of the timestamp you’re creating, you should be looking at the [fphp]date[/fphp] function instead.
Thanks, so how would I go about doing that? Sorry I’m very new to PHP
does $Message->scheduledDatetime
accept a DateTime
object? because that’s what date_timestamp_set()
returns. otherwise you’ll need DateTime::format()
or date_format()
.
See http://php.net/manual/en/class.datetime.php#datetime.constants.types
Assuming you fix it to create $date properly as a datetime then the following is how your assignment should read:
$Message->scheduledDatetime = $date->format(DATE_RFC3339);
Thanks guys, I’ll change the format.
It’s probably not the best way of doing it but I changed it to this:
$Message->scheduledDatetime = $_POST['year']."-".$_POST['month']."-".$_POST['day']."T".$_POST['hour'].":".$_POST['minute'].":39+00:00";
felgall
October 22, 2014, 6:20pm
10
definitely not.
That way allows anyone to set the field to almost anything.
For example (since you don’t validate any of the inputs before using them):
rubarb-custard-mustardTcar:bike:cart:smith:jones:database delete call:39+00:00
1 Like
It’s definately boat past car, your watch must be slow felgall.
But yes. What you’re doing can be greatly mis-used without validation. (EX: All of the fields you list should be Integers.)
I didn’t realise that, I’ll change them, thanks
Honestly your code is very messy and should be refactored/redesigned. 4-5 dimensional arrays are code smells are definitely aint good programming practices. I am not sure who wrote the initial program, but clearly some work needs to be done with the codebase.