PHP: Convert date to Javascript date (January = zero) - php

I'm using Highcharts and need the dates formatted like this:
UTC.(2016,11,01) // for December 1st 2016
Is there an easy way to do this? Because
date('Y-m-d', strtotime('-1 month'))
gives me the wrong output for January.

You don't need sub the month. Just use other format as ATOM/RFC3339. It's fully supported by javascript Date class
$date = new DateTime('2016-01-01');
echo $date->format(DateTime::ATOM);
Result:
2016-01-01T00:00:00-05:00
Javascript:
var date = new Date('2016-01-01T00:00:00-05:00');
console.log(date)
As I'm live in Brazil, my browser show as
Fri Jan 01 2016 03:00:00 GMT-0200 (Horário brasileiro de verão)
If you have any other format as input, you could use DateTime::createFromFormat and the rest still the same
$date = DateTime::createFromFormat('m/d/Y' , '12/01/2016');

Related

Modify DateTime on Month short name formatted date string

I'd like to use the datetime->modify function on a date string that's formatted like "21 Jan 2016". When I use the datetime->modify and add 1 day, it gives me a result of 30 Apr 2017. I know that if I don't use the short month name and use a number instead (i.e. 01), it will work fine but I would like to get it work this way with short month name. Is this possible?
Please see code below:
<?php
$date = "21 Jan 2016"; // this is my date string
$newdate = new DateTime($date );
$date2 = $newdate->modify('+1 day'); // add 1 day to date string
echo $date2->format("d-M-Y");
?>
RESULT is:
30-Apr-2017
RESULT WANTED
22-Jan-2016
The problem is that you are trying to create a DateTime object from a non-ISO format. That's that part that is not working.
Take a look at: http://php.net/manual/ro/datetime.createfromformat.php
You will need to have something like
DateTime::createFromFormat('d M Y', '21 Jan 2016');
Full example:
$tomorrow = DateTime::createFromFormat('d M Y', '21 Jan 2016')->modify('+1 day')->format("d-M-Y");
echo($tomorrow);
The format of the $date variable is incorrect. Off the top of my head, there are two easy ways to fix this:
Set $date = "Jan 21, 2016"
Set $date = "21-Jan 2016"
More options: https://secure.php.net/manual/en/datetime.formats.date.php
Your date format was wrong. That's all.

Get Unix timestamp of specific date after another specific time

I can get the for example 19 March of specific date with this code:
$date = strtotime(" 19 March", $current_time);
For example if I gave the unix timestamp of 1st of January of 2010 as an input, It gave me 19 March of 2010. But also if I gave the unix timestamp of 20 March of 2010,I still get 19 March 2010. What I want is to get the next 19 March which in this case, It would be 19 March of 2011.
How can I do that?
Using PHP DateTime this can be achieved as follows:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
You can do something like as
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
You should first get year from specified date. Then after you can create 19 march date with year and use strtotime() to get timestamp.
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
You can specify how many days or week you want to add or subtract from a day, as well as set the time with these functions
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();

Format the formatted date for mysql

How can I format this date 01 August, 15 for mysql DATE field. So I need this 2015-08-01 format from 01 August, 15 this format in PHP. Tried following but not work
echo date('Y-m-d',strtotime('01 August, 15'));
It is because strtotime() does not understand what 01 August, 15 means. Try it:
var_dump(strtotime('01 August, 15')); // false
The 15 at the end is too ambiguous; it could be the day of the month or a short year.
The easiest way to make this work is probably to use DateTime::createFromFormat, like so:
$date = '01 August, 15';
$parsed = DateTime::createFromFormat('d F, y', $date);
echo $parsed->format('Y-m-d');
If you control the format of the date then you could also make it easier to parse. Formatting it like 01 August 2015 would work, for example.
First remove the , out of the date and then use the strtotime function.
So:
$date = "01 August, 15";
$date = str_replace(",", "", $date);
echo date("Y-m-d",strtotime($date));

Time zone confusion processing javascript generated date in php

I am located in the (PDT) time zone at time Sat May 11 2013 20:58:51 (my time) I generated a date/time using the following code.
var date = new Date();
alert(date);
This returns the result
"Sat May 11 2013 20:58:51 GMT-0700 (PDT)"
If I then post this date to a php script which processes it in the following way:
$date = date('Y-m-d H:i:s', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
echo json_encode($date);
I get the result
"2013-05-12 03:58:51"
Not what I expected. I only get the expected time if I get rid of the "GMT-0700 (PDT)" part from my date/time. So I have two questions.
Can anyone tell me how to generate a date of this format but with out the "GMT-0700 (PDT)" part in javascript without using string functions/regex?
Is my browser giving me the wrong time zone, or is php interpreting the time zone incorrectly. In either case, why?
JavaScript always take your local server timezone, while PHP is converting your date to UTC, so you need to set your server time zone before using strtotime as follow,
date_default_timezone_set('America/Los_Angeles');
$date = date('Y-m-d H:i:s', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
echo json_encode($date);
DEMO.
Or if you would like to convert your js date to UTC than use,
var date = new Date();
var utcdate = date.toUTCString()
$date = date('r', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
1- echo date("l F j, Y, H:i s");
2-
date_default_timezone_set('America/Los_Angeles');
$postedDate = "Sat May 11 2013 20:36:24 GMT-0700 (PDT)";
$date = date('Y-m-d H:i:s ', strtotime($postedDate));
echo json_encode($date);

strtotime - a second before midnight

I attempted this:
$date_string = strtotime('6 Mar, 2011 23:59:59');
But I think PHP can't interpret that for some reason as it returned empty. I tried this:
$date_string = strtotime('6 Mar, 2011 midnight');
The above worked but I need it to be a second before midnight i.e. the last second of the day. How can I get strtotime to return this without changing the 6 Mar, 2011 part?
Hope this helps. I used this and it gives todays timestamp just before midnight. Counter intuitive.
$today_timestamp = strtotime('tomorrow - 1 second');
It works for me if I use March 6, 2011 23:59:59. Any chance of changing the input format?
Other than that, you could of course subtract 1 second from the timestamp. Note however that you need to use March 7:
$date_string = strtotime('7 Mar, 2011 midnight') - 1;
Why not use mktime?
mktime(23,59,59,3,6,2011);
If you're on PHP 5.3 or greater, you could use the DateTime class.
The createFromFormat function allows you to manually specify how to parse your input date string.
$date = '6 Mar, 2011 23:59:59';
$timestamp = DateTime::createFromFormat('d M, Y H:i:s', $date)->getTimestamp();

Categories