php string date to datetime format [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to convert string date time to datetime format. bellow is my code
date_default_timezone_set("Asia/Singapore");
$EventDate = '2018-09-14 16:30:00.000';
$createDate = new DateTime($EventDate);
$NewEventDate = $createDate->format('Y-m-d h:m:s');
but when i convert string date to datetime, i get time wrong bellow is the output
STRING EVENT DATE : 2018-09-14 16:30:00.000
CONVERTED EVENT DATE : 2018-09-14 04:09:00
the time 16:30:00 shows 04:09:00 which is wrong

try this
$NewEventDate = $createDate->format('Y-m-d H:i:s');

Use i instead of m for minute
date_default_timezone_set("Asia/Singapore");
$EventDate = '2018-09-14 16:30:00.000';
$createDate = new DateTime($EventDate);
$NewEventDate = $createDate->format('Y-m-d H:i:s');

date_default_timezone_set("Asia/Singapore");
$EventDate = '2018-09-14 16:30:00.000';
$createDate = new DateTime($EventDate);
echo $NewEventDate = $createDate->format('Y-m-d H:i:s');

Related

Why it does not return the right date? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
If somebdoy may help me?, I have a problem with method mktime.
Here is my code:
$now = new DateTime("now");
echo $now->format('Y-m-d')."\n";
$nowYear=intval($now->format('Y'));
var_dump($nowYear)."\n";
$nowMonth=intval($now->format('m'));
var_dump($nowMonth)."\n";
$nowDay=intval($now->format('d'));
var_dump($nowDay)."\n";
$end=date('Y-m-d',mktime(0,0,0,'2021','03','01'));
echo $end."\n";
The value returned by $end is wrong. It returns 2169-05-03 instead of 2021-03-01.
mktime
mktime ( int $hour , int|null $minute = null , int|null $second = null ,
int|null $month = null , int|null $day = null , int|null $year = null ) : int|false
Last 3 arguments should be month, day and year. You passed '2021','03','01'

php datetime-local format replace can you help me? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
can you please help me?
i need to change
2017-12-31 11:45:00
to
2017-12-31T11:45
using php function.
I need this datetime-local format for putting in a input value.
This might do it for you:
<?php
$date = '2017-12-31 11:45:00';
$newdate = date('Y-m-d\TH:i', strtotime($date));
echo $newdate; // 2017-12-31T11:45
;?>
Documentation about strtotime: http://php.net/manual/en/function.strtotime.php
NOTE:
strtotime max date is '19 Jan 2038 03:14:07 UTC'.
New solution: PHP 5 >= 5.2.0, PHP 7
Please consider to use the new DateTime function: http://php.net/manual/en/datetime.construct.php
Example new DateTime:
<?php
$d = new DateTime("9999-12-31");
$d->format("Y-m-d"); // "9999-12-31"
$d = new DateTime("0000-12-31");
$d->format("Y-m-d"); // "0000-12-31"
$d = new DateTime("-9999-12-31");
$d->format("Y-m-d"); // "-9999-12-31"
?>
This can be done quite simply using the DateTime object provided with PHP
<?php
$dt = DateTime::createFromFormat('Y-m-d H:i:s', '2017-12-31 11:45:00');
$newFormat = $dt->format('Y-m-d\TH:i');
echo $newFormat;
Result:
2017-12-19T11:45

fetching only year and month from a date saved in the format of strtotime [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
My date string value is 1478025000. want to get year and month in the string format itself from this string,is it possible? the day should be 0 and next month and next year like that.
my desired output should be 1477852200
Try something like this:
$epoch = 1478025000;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m'); // Display as year and month: YYYY-MM
echo $dt->format('m-Y'); // Display as month and year: MM-YYYY
If you want to get just the year and month as an epoch, try something like:
$epoch = 1478025000;
$dt = new DateTime("#$epoch"); // convert UNIX timestamp to PHP DateTime
$year = $dt->format('Y');
$day = $dt->format('n');
$answer = new DateTime();
$answer->setDate($year, $month, 0);
$answer->setTime(0, 0, 0);
echo $answer->getTimestamp();

Create date time interval [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to create DateTime interval to compare it with date difference. Is something like this possible?
$week = new \DateTime("1 week");
Well, you can use DateInterval class instead. While you cannot pass the relative timeformats such as '1 week' into its constructor directly, there's a useful static method createFromDateString() that supports such syntax:
$weekInterval = \DateInterval::createFromDateString('1 week');
The problem is that DateInterval instances are not comparable in PHP (check this feature request for details):
$someInterval = (new \DateTime('2015-09-14'))->diff(new \DateTime('2015-09-13'));
var_dump($weekInterval > $someInterval); // false somehow
DateTime instances are comparable, however. So one possible workaround is making a comparison between the results of adding two DateInterval to the same fixed date. For example:
$now = new \DateTimeImmutable();
var_dump($now->add($weekInterval) > $now->add($someInterval)); // true
Eval.in demo.
I think what you are looking for is the diff(); function in the DateTime class.
$dateOne = new DateTime('2015-10-11');
$dateTwo = new DateTime('2014-10-13');
$interval = $dateOne->diff($dateTwo);
echo $interval->format('%R%a days');
Comparing UNIX TimeStamp
$dateTime = new DateTime("#" . $someOtherTimeStamp);
$endTime = new DateTime("#" . time());
$interval = $dateTime->diff($endTime);
Reading Material
diff();

How to Convert M to m in date php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to convert the date like this
$original_date = date("M/d/Y"); // the output of that is Aug/27/2013
to "08/27/2013"
Like this:
date('m/d/Y', strtotime($original_date));
strtotime can convert pretty much anything reasonable you give it into a Unix timestamp, even stuff like "Next Friday".
EDIT
Funny, but seems that strtime doesn't work with a date formatted like that... The first thing that came to my mind was to replace those slashes with spaces, using str_replace or implode/explode or whatever works for you...
$newDate = date('m/d/Y', strtotime(str_replace('/', ' ', $origDate)));
$newDate = date('m/d/Y', strtotime(implode(' ', explode('/', $origDate))));
Simply by passing it back to the date function
$original_date = date("M/d/Y");
$new_date = date('m/d/Y', strtotime($original_date));
Simply change the format string:
$originalDate = date("m/d/Y"); // = 08/27/2013
Or use DateTime and specify the input format to avoid ambiguity:-
$dateString = \DateTime::createFromFormat('M/d/Y', $origanalDate)->format('m/d/Y');

Categories