Im using JQuery Full Calendar Plugin. Code:
$('#mycalendar').fullCalendar({
** options **
events: function(start, end, callback) {
$.ajax({
url: '/myloader/',
dataType: 'json',
data: {
// our hypothetical feed requires UNIX timestamps
start: Math.round(start.getTime() / 1000),
end: Math.round(end.getTime() / 1000)
}
*** more stuff
});
now on myloader php side when i try to get start and end dates here is what i get:
var_dump(date('m/d/Y H:i:s', $_GET['start']), date('m/d/Y H:i:s', $_GET['end']));
this returns:
string(19) "01/27/2013 06:00:00"
string(19) "03/10/2013 06:00:00"
why is it 6:00:00 ? i want it to be 00:00:00 for start and 23:59:59 for end
I know i can hack through it using PHP but is there a reason why full calendar returns such date?
If i use PHP i can get desired results using:
$start = strtotime(date('m/d/Y', $start) . ' 00:00:00');
$end = strtotime(date('m/d/Y', $end) . ' 23:59:59');
but i dont want to do it on PHP side is there a way full calendar to give correct time?
If its a timezone issue how can it be fixed?
thanks
How about this:
var_dump(
date('m/d/Y H:i:s', strtotime(date("m/d/Y",(int)$_GET['start'])),
date('m/d/Y H:i:s', strtotime(date("m/d/Y",(int)$_GET['end']))
);
Related
I'm trying to retrieve metrics from Google My Business API.
However I cant figure out what to pass as a timestamp for the time periods.
The error I'm getting is this...
Invalid value at 'basic_request.time_range.end_time' (type.googleapis.com/google.protobuf.Timestamp),
Field 'endTime', Invalid data type for timestamp, value is 1606780800
My code is this
$time = new \Google_Service_MyBusiness_TimeRange;
$start = strtotime("2020-01-01");
$end = strtotime("2020-12-01");
$time->setStartTime($start);
$time->setEndTime($end);
In the class for the TimeRange it shows they simply must be timestamps
class Google_Service_MyBusiness_TimeRange extends \Google_Model
{
protected $internal_gapi_mappings = array( );
/* #params Unix Timestamps */
private $endTime;
private $startTime;
However... I was looking at this.
https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.Timestamp
And it shows that you have to pass something on the lines of Timestamp(seconds, nanos).
So it seems like google wants an array of the seconds, and the nano seconds??
Heres other docs to help
https://developers.google.com/my-business/reference/rpc/google.mybusiness.v4#google.mybusiness.v4.TimeRange
Anyone run into this problem?
The documentation you link to says:
Range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z.
Suggesting that you should use a similar format.
$time = new \Google_Service_MyBusiness_TimeRange;
$start = \DateTime::createFromFormat(
"y-m-d H:i:s",
"2020-01-01 00:00:00",
new \DateTimeZone("UTC")
)
->format("Y-m-d\TH:i:s\Z");
$end = \DateTime::createFromFormat(
"y-m-d H:i:s",
"2020-11-30 23:59:59",
new \DateTimeZone("UTC")
)
->format("Y-m-d\TH:i:s\Z");
$time->setStartTime($start);
$time->setEndTime($end);
I wasn't able to find much about this library online, but what little there is does follow this format. "Z" at the end indicates UTC, and I've hard-coded it in my example. It should be able to get replaced with your local timezone, but you may have to try perhaps "O" or "P" in the format string.
Here is some working code:
Note, $startDate and $endDate are just strings passed in from a jQuery calendar, ex "12/27/2020".
The key here is to use DATE_ATOM, which produces a timestamp in the format 2020-12-27T13:22:12+00:00
$gmbStartDate = date(DATE_ATOM, strtotime($startDate . " 12:01 AM "));
$gmbEndDate = date(DATE_ATOM, strtotime($endDate . " 11:59 PM "));
$time = new Google_Service_MyBusiness_TimeRange();
$time->setStartTime($gmbStartDate);
$time->setEndTime($gmbEndDate);
$basicMetricsRequest->setTimeRange($time);
Note that the timestamps are expressed as UTC, so in this example, the insights may be slightly off based on the time zone for the business. If you want to get the insights for your time zone, you'll have to add/subtract the appropriate number of seconds for your timezone.
Hello guys I'm using to strtotime to convert the date but the problem that I'm facing is it sets the time to 19:00:00 which is wrong. I am sending a date string and then giving it to strtotime and it set the time itself. Right now my time is 11:58 but it is storing the time as 19:00. Please tell me why am I facing this error. Here is the code: strtotime($s_date) $s_date contains only a date string strtotime sets the time
the date string that I'm sending is this 03/04/2016
HTML
<input type="text" class="form-control" id="starts" data-container="#addNewEvent"
data-plugin="datepicker">
Ajax call
$.ajax({
type: "POST",
url: base_url + "apps/calendar/insertCalendar",
async : false,
dataType: 'html',
data: {
'start': $('#starts').val(),
'end': $('#ends').val(),
'title': $('#addTitle').val(),
'description': $('#addDescription').val(),
'type':type
},
success: function(mark_up){
toastr.options = {positionClass: 'toast-top-center'};
toastr.success('Your reminder has been created', 'Reminder created');
window.location.reload();
}
});
Controller Method
public function insertCalendar(){
$s_date = $_POST["start"];
$p = strtotime($s_date);
error_log($p)
}
one thing more I'm stroing it in mongodb so the object that I get from strtotime i convert them into new MongoDate(strtotime($s_date))
First we need to set first timezone
date_default_timezone_set('Asia/Karachi');
then you can get time and date using below code:
echo date('Y-m-d h:i:s'); /* Current date and time*/
echo date('Y-m-d'); /* Current date */
echo date('h:i:s'); /* Current time */
If you want to convert date into required format.
For Example. 03/04/2016, so use
$s_date = '03/04/2016';
echo date('Y-m-d',strtotime($s_date)); /* Convert date into mysql date format */
You will get date according to Y-m-d format of mysql like 2016-03-04.
Now you want user provided date with current time, then you should use
$s_date = '03/04/2016'.' '.date('h:i:s');
echo date('Y-m-d',strtotime($s_date));
This will solve you problem.
I'm using the datetimepicker jquery plugin and have the following code to display it:
var dateToday = new Date();
$('#post_date_picker').datetimepicker({
minDate: dateToday,
timeFormat: 'hh:mm p z',
timezoneList: [
{ value: -300, label: 'Eastern'},
{ value: -360, label: 'Central' },
{ value: -420, label: 'Mountain' },
{ value: -480, label: 'Pacific' }
]
});
Since I'm displaying it in 12 hour format instead of 24 and using the min date if the server time is already PM I can select the time and it outputs it like 05/27/2015 08:50 p -0500
I then do the following code to get the date ready to be inserted into the database:
$date_time = strtotime($_POST['post_date']);
$post_date = date('Y-m-d H:i:s', $date_time);
The problem is strtotime isn't recongining the p for PM and is inserting the date basically 12 hours previously. How can I do this?
Set timeFormat: 'hh:mm tt z'.
And in php use date_create & date_format ( $date_time,'Y-m-d h:i:sA')
See the difference between( strtotime / date_create ).
// method using date_create
//$date=$_POST['post_date'];
$date='05/27/2015 08:50 p -0500';
$date_time=date_create($date);
$post_date=date_format($date_time,'Y-m-d H:i:s');
echo $post_date."<br>";//outputs 2015-05-27 08:50:00
//method using strtotime
$date_time = strtotime($date);
$post_date = date('Y-m-d H:i:s', $date_time);
echo $post_date;//outputs 2015-05-27 13:50:00
strtotime can take many string parameters, may be when you pass the date as '05/27/2015 08:50 p -0500' some conflict is occurring resulting in wrong interpretation of time.
I want to convert 1373892900000 to Monday 2013/07/15 8:55 AM in Codeigniter.
However, I keep receiving a totally different result by converting the timestamp using the function i have written, please note:I need to change the dates according to different timezones, that is why I want to write it this way:
public function time_convert($timestamp){
$this->load->helper('date');
date_default_timezone_set('UTC');
$daylight_saving = TRUE;
$timezone = "UM4"; //toronto or new york timezone
$time = gmt_to_local($timestamp, $timezone, $daylight_saving);
$final_time = standard_date('DATE_RFC822', $time);
return $final_time;
}
Result from the above function is: Sat, 08 Dec 06 01:40:00 +0000
And if I don't put date_default_timezone_set('UTC'); in the above function, I get this date instead Sat, 08 Dec 06 02:40:00 +0100. My codeigniter seems to default the timezone to Europe/Berlin.
Can anyone please help me correct any of the mistakes I might have made?
Why not just use PHP's date function?
public function time_convert($timestamp){
return date('l Y/m/d H:i', $timestamp);
}
For different timezones use a DateTime object:
public function time_convert($timestamp, $timezone = 'UTC'){
$datetime = new DateTime($timestamp, new DateTimeZone($timezone));
return $datetime->format('l Y/m/d H:i');
}
Think that should work. Note: I tihnk you need at least PHP version 5.20 for the TimeZone class.
<?php
$time_str=1373892900000;
echo gmdate("fill with your format", $time_str);
?>
your format = format your time in php, reading this page for details.
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.gmdate.php
Appears as though an invocation of standard_date with the DATE_ATOM format may sort you:
echo unix_to_human(time(), true, 'us'); # returns 2013-07-12 08:01:02 AM, for example
There are a whole host of other options for the format, enumerated on the linked page.
This how to covert timestamp to date very simple:
echo date('m/d/Y', 1299446702);
to convert timestamp to human readable format try this:
function unix_timestamp_to_human ($timestamp = "", $format = 'D d M Y - H:i:s')
{
if (empty($timestamp) || ! is_numeric($timestamp)) $timestamp = time();
return ($timestamp) ? date($format, $timestamp) : date($format, $timestamp);
}
$unix_time = "1251208071";
echo unix_timestamp_to_human($unix_time); //Return: Tue 25 Aug 2009 - 14:47:51
if you want to convert it to a format like this: 2008-07-17T09:24:17Z than use this method
<?php
$timestamp=1333699439;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);
?>
for details about date:
http://php.net/manual/en/function.date.php
Your timestamp is coming from javascript on the client, I would guess, because it appears to be in milliseconds. php timestamps are in seconds. So to get the answer you want, first divide by 1000.
Showing the full year would have made the issue more obvious, as you would have seen the year as 45,506.
Hi I'm using php and sql through odbc to write a program and i hav got abit stuck in a part where i want to display the current date/time in the format date('Y-m-d H:i:s) but it only displays the gmt time. I want to add 8hours to it.Can any of you b able to help me.Thank you so much
Check out date_default_timezone_set. You can do something like:
date_default_timezone_set('America/Los_Angeles');
print 'Current datetime is: ' . date('Y-m-d H:i:s');
You could use that to set the timezone to whatever timezone you need time to be at, and then use date normally. Alternatively, you can do this, using strtotime:
print 'Current datetime is: ' date('Y-m-d H:i:s', strtotime('+8 hours'));
If you're looking for a way to display a timestamp in a user's local time, you can use JavaScript:
function showtime(t)
{
if (t == 0)
{
document.write("never");
return;
}
var currentTime = new Date(t);
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
document.write();
if (minutes < 10){
minutes = "0" + minutes;
}
if (seconds < 10){
seconds = "0" + seconds;
}
document.write(month + "/" + day + "/" + year + " " +
hours + ":" + minutes + ":" + seconds + " ");
if(hours > 11){
document.write("PM");
} else {
document.write("AM");
}
}
Then if you need to display a time, just make a call to it in the HTML and splice in the value from PHP:
<script type="text/javascript">showtime(<?=$time."000"?>)</script>
I would steer clear of the timezone method.
If i understood correctly, you want to add time, thus change it. An example could be, A task has been created NOW, and must be complete in 8 hours. The timezone method would only change the display of the date and time. Only change the timezone setting if you know your visitor's timezone, and datetime's must be shown relative to them.
Now: 1234418228 is 2009/02/12 00:57:08 in Montreal or 2009/02/11 09:57:08 in San Francisco. It's the exact same moment.
Appending to the first answer, date() and strtotime() are your friends.
strtotime( "+8 hours", $now )
$now being a timestamp of when it's supposed to relate to. So if your start time isn't time(), you can still use that. eg
strtotime( "+8 hours", strtotime( "2009/03/01 00:00:00" ); (8AM on 2009/03/01)
However, when dealing with intervals counted in weeks, or less, i prefer doing it 'mathematically'
$StartTime = strtotime( "2009/03/01 13:00:00" );
$EndTime = $StartTime + ( 8 * 60 * 60 );
date( "Y/m/d H:i:s", $EndTime ) ==> "2009/03/01 21:00:00"
3600 seconds in an hour, 86400 in a day.
You can't use this method for months, quarters or years because the number of seconds they last varies from one to the next.
If you want to use time for a certain timezone, then using date_default_timezone_set() is preferred. anyway you can provide the date() function another parmater: int timestamp. an integer representing the timestamp you would like date() to return the information about.
so if you would like to show date('Y-m-d H:i:s') for now you can use this:
$now = date('Y-m-d H:i:s', time() ); // time() returns current timestamp.
// if you omit the second parameter of date(), it will use current timestamp
// by default.
$_8hoursLater = date('Y-m-d H:i:s', time()+60*60*8 );
$_8hoursBefore = date('Y-m-d H:i:s', time()-60*60*8 );