Set date range for lineItem using GoogleAds API - php

I have been having trouble trying to figure out how to set a range of dates for a given lineItem using the google ads php client library. Basically, what I want to do is make a line item to be available for a specified start date and an end date but no success. In their example they have this snippet:
$lineItem->setStartDateTimeType(StartDateTimeType::IMMEDIATELY);
$lineItem->setEndDateTime(
AdManagerDateTimes::fromDateTime(
new DateTime('+1 month', new DateTimeZone('America/New_York'))
)
);
They're setting a start date for the line item to IMMEDIATELY, and and end date to 1 month from the time of creation. I tried passing to AdManagerDateTimes::fromDateTimeString a valid ISO 8601 string and no luck (GAM spits an error). Tried creating a DateTime() instance and passing it to the code above, nothing. I'm not too experienced in php and maybe this is way easier than i think it is but I'm stuck.
Any tips please?
Thank you

i don't know if you already solve this issue, but you can change in the new DateTime include the date you want
$lineItem->setEndDateTime(
AdManagerDateTimes::fromDateTime(
new DateTime('2023-02-12')
)
);

Related

Php time and datatime object

So,
I know a lot of requests and question has been askeb about this subject but none really worked for my case... I'm working on a liscensing api with php (supposed to be easy) and I get a string date (2000-01-01) from my db and the length of the subscription. So I'm creating a DateTime Object with it using this :
$created_at = date_create($result["created_at"]);
date_add($created_at, date_interval_create_from_date_string($result["length"]." days"));
But for some unknowed reason, It seems I can't get the current date in a DateTime object so I can just compare them with <>=. Even if I use date_sub() or date_diff() It still require two DateTime object. I'm really deseperate at this point so I figured I could ask for some help.
Hope I didn't miss anything obvious
You can use the 'now' attribute,
$today = new DateTime('now'); to get the current time.
Don't forget to set your timeregion in your php.ini to be able to get the right time.
And if you want to compare them, you can use date_diff and then
$var->format('%r') to get the value.
%r is going to be empty if the result is positive.
Good luck!

Generate appropriate pattern for IntlDateFormatter()->format() based on locale in PHP

I'm trying to rely on IntlDateFormatter to return the current date in a locale-based format. This means DD/MM/YYYY for it_IT, de_DE, fr_FR... or MM/DD/YYYY for en_US and so on for all the possible locales.
I'm trying to follow the same solution I used to retrieve the current month's name:
$formatter = new IntlDateFormatter("it_IT", IntlDateFormatter::FULL, IntlDateFormatter::FULL);
$formatter->setPattern("MMMM");
return $formatter->format($this);
This code correctly returns "January", "gennaio" etc.
The problem is, I cannot figure out the right pattern to get the date in the current locale format. The ICU User Guide mentions the DateTimePatternGenerator class, and this looks like it! But I cannot find it in PHP.
I would like to avoid a custom, huge switch-case and rely on built-in function instead.
It must be pure PHP.
Any help?
I'm not sure if you want to get pattern from ICU itself, or just format your DateTime. Here you have both solutions.
Basics:
You're doing well with you actual code for retrieving month name. IntlDateFormatter is pretty customizable, and it all depend on options you gave it.
Getting date with year, month and day depending on locale:
$locale = "en_GB";
$dateType = IntlDateFormatter::SHORT;//type of date formatting
$timeType = IntlDateFormatter::NONE;//type of time formatting setting to none, will give you date itself
$formatter =new IntlDateFormatter($locale, $dateType, $timeType);
$dateTime = new DateTime("2015-02-28");
echo $formatter->format($dateTime);
will give you
28/02/2015
You can play with other options of IntlDateFormatter. There's SHORT, MEDIUM, LONG, FULL and NONE - you can read about them and check example ouput here.
Getting pattern from formatter
$formatter->->getPattern();
will give you something like
dd/MM/yyyy
I hope this will help you. I've been struggling with it for a long time to do it properly :)

How to create DateTime object from string in symfony2/php

In a DB table I have several fields with datetime as field type. So I need to persist data only as date time object.
From a form I get date time as string like
2012-10-05 17:45:54
Now when ever I persist my entity I get following error:
Fatal error: Call to a member function format() on a non-object in
..\DateTimeType.php on line 44
I tried with
$protocol->setStartedAt(strtotime($post['started_at']));
or
$from = \DateTime::createFromFormat('yy-mm-dd hh:mm:ss', $post['started_at']);
$protocol->setStartedAt($from);
or just
$from = new \DateTime($post['started_at']);
$protocol->setStartedAt($from);
The last code works but it does not uses the timestamp passed as arguement but just gets the current time.
Any ideas?
I always create a DateTime object with its constructor, in your case it would be:
$protocol->setStartedAt(new \DateTime($post['started_at']));
if this works but does not use the timestamp posted you probably do not have the value in $post['started_at']. Try debugging it or just do the dirty trick:
die($post['started_at']);
For the sake of future readers who surely will someday encounter this problem (this is the first post if you google "symfony 2 datetime from string"), keep in mind that in Symfony 2 the DateTime object does NOT accept a string with that format : "d/m/Y H:i:s", and probably doesn't support many others either.
For the sake of not becoming mad at that, I've actually found out that the easiest and safest solution to avoid such errors is this one:
First, get your date string from whatever kind of request you are doing (In my case a generic AJAX request) and convert it to a DateTime Object, this example assumes that we need to create a dateTime object for 25/04/2015 15:00, which is the format of the jQuery UI italian DateTimePicker (that's just an example):
$literalTime = \DateTime::createFromFormat("d/m/Y H:i","25/04/2015 15:00");
(note: use \ to use php's DateTime object, else you will be using Symfony's datetime object that will throw you an exception)
Then, once you did it, create a date string using the comfort format function, by giving to the first parameter the output format expected (Y-m-d H:i:s):
$expire_date = $literalTime->format("Y-m-d H:i:s");
In this way you are 100% sure that whatever kind of format you are passing or receiving this will properly be converted and you won't get any kind of exception from the DateTime symfony object, as long as you provide what you are expecting as an input.
Knowing that this post is actually quite old, I've just decided to post that because I didn't find any other valuable source but this one to understand where the problem could have been.
Please note that the best solution is still to send the datetime string in the correct format already, but if you literally have no ways to do that the safest way to convert such a string is the one above.
How about createFromFormat?
http://uk.php.net/manual/en/datetime.createfromformat.php
$from = DateTime::createFromFormat($post['started_at'], 'Y-m-d H:i:s');

Call to undefined method DateTime::setTimeStamp()

I've been using setTimeStamp to convert a Unix Timestamp to a datetime in the following way:
$startHireConverted = strtotime($startHire); // converts start hire to time
$endHireConverted = strtotime($endHire); // converts end hire to time
$startdt = new DateTime();
$startdt->setTimeStamp($startHireConverted);
$mysql_startdate = $startdt->format("Y-m-d H:i");
This was working nicely, but recently I've put the website live and the version of PHP can only be 5.2.12 which doesn't support the setTimeStamp method.
I've tried changing setTimeStamp to format which is getting rid of the errors and converting the datetime but it is changing it to the current datetime - 5 hours for some reason rather than the date stored in $startHire.
$startdt->format($startHireConverted);
Any ideas on how to get around this problem?
$startHire starts out as a string version of datetime.
Thankyou
I don't think you need to do the step with unix timestamp at all:
$startdt = new DateTime($startHire);
PHP manual: DateTime::__construct
But that might depend on the format you're getting $startHire in. See Supported Date and Time Formats.

Date format on OpenX API addCampaign method

I'm creating a campaign through the OpenX API, and so far so good but i need to set an end date to the campaign, via the endDate parameter; the thing is: i don't know what i should bind to the parameter on the API call. I tried using this: $date = date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s") . $date_threshold)); where $date_threshold is something like "+1 month", but the endDate won't appear on the OpenX panel.
What kind of data do i need to bind to that parameter so it's correctly inserted on the OpenX database?
Thank you in advance.
Glad you got it to work -- Just as a note: I'm not sure how you handle the general XML-RPC communication, but what I do is pass a date string into PEAR's XML-RPC, something like:
new XML_RPC_Value('20100413T00:00:00', 'dateTime.iso8601')
Turns out it is a simple DateTime(), so nothing of that would work, because the API wants a Date object, not string!
So, the code i sent on the question turns into:
$date = new DateTime();
$date.modify($date_threshold);

Categories