converting a SQL server date time to mysql date time [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert DateTime to VarChar
Example
How do you convert a SQL datetime (for example 08/14/2012 1:05:18 PM ) to the mysql way ( that is Y-m-d H:i:s )?

Depends on where you are wanting to convert it.
To convert in your SQL directly:
convert(varchar(23), getdate(), 121)
PHP using the date and strtotime functions:
date("Y-m-d H:i:s ", strtotime("08/14/2012 1:05:18 PM"));

Change the format of the date in PHP like so:
<?
$date = date("Y-m-d H:i:s ", strtotime("08/14/2012 1:05:18 PM"));
echo $date;
?>

$var = "08/14/2012 1:05:18 PM";
echo date('Y-m-d H:i:s', strtotime($var));

Related

Convert date format from Y-m-d H:i:s into dmy [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a date time in string with format Y-m-d H:i:s like this:
$dateTime = '2018-07-06 18:53:21';
i want to conver it into dmy format, like this:
$convertedDateTime = $this->convertDateTime($dateTime);
echo $convertedDateTime;
and the result i expected from above echo is 060718, how can i achieve this?
You can use date_format like this:
echo date_format($date,"Y/m/d H:i:s"); //e.g. 2013/03/15 00:00:00
echo date_format($date,"dmy"); //e.g. 150313 -- Y capital would return 2013
there's two methods to use:
date - https://secure.php.net/manual/en/function.date.php
usage example:
$myDate = '2018-07-06 09:49:00';
$myDate = date('d-m-y', strtotime($myDate));
but more often than not you'll want to use DateTime https://secure.php.net/manual/en/class.datetime.php:
$myDate = DateTime::createFromFormat('Y-m-d H:i:s', '2018-07-06 09:49:00');
$$newDate = $myDate->format('d-m-Y');
\DateTime::createFromFormat('Y-m-d H:i:s', '2018-07-06 18:53:21')->format('dmy');

convert Y/M/d H:i to db format in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have code:
$date= "2016/Apr/16";
$time="5:00 PM";
I want to convert it to DB format like 2016-04-16 17:00:00 in php .
I am not getting exact code from anywhere.
This way will solve your problem.
<?php
$date= "2016/Apr/16";
$time="5:00 PM";
$fullDate = str_replace('/','-',$date) . " " . $time;
$newDate = date("Y-m-d H:i:s", strtotime($fullDate));
echo $newDate;
?>
Result
More Examples;
http://www.w3schools.com/php/func_date_strtotime.asp
Convert date format yyyy-mm-dd => dd-mm-yyyy
Normally you would use the following code:
date('Y-m-d H:i:s', strtotime($date.' '.$time));
If it returns the year 1970, then the input date (the one from $date variable) is not formatted according to a standard that can be recognised by php.

PHP change date format (date coming from MySQL's 'CURRENT_TIMESTAMP' [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I tried a few things and so far and no success.
I want to make dates that are yyyy-mm-dd look like mm/dd/yyyy
Initial value example, taken from MySQL: 2015-08-21 14:46:00
Here's what I got:
foreach ($draftOrders as $key => $value) {
$timestamp = strtotime($value['last_modified']);
// this one works (leaving it unchanged)
// however the slashes are not replacing the dash
// $new_date_format = date('Y/m/d H:i:s', $timestamp);
// fails, date becomes 1970-01-01 12:00 am
$new_date_format = date('m/d/Y H:i:s', $timestamp);
$draftOrders[$key]['last_modified'] = $new_date_format;
}
To perform that sort of date formatting in mysql you might want to look at the following:-
select date_format(`date`,'%Y/%m/%e %H:%i:%s') from `table`
try this
echo date('m/j/Y', strtotime('2015-08-21 14:46:00'));
http://php.net/manual/en/function.date.php

Correct way of converting DateTime string in timestamp [duplicate]

This question already has answers here:
datetime to timestamp [duplicate]
(2 answers)
Closed 8 years ago.
Which is correct way of converting datetime string in format like '2014-12-06 07:45' in timestamp ?
I make like
date( "Y-m-d H:i", '2014-12-06 07:45' );
but time is zero...
Flexible format way:
$date = \DateTime::createFromFormat('Y-m-d H:i', '2014-12-06 07:45');
echo $date->getTimestamp();
This should work for you:
echo strtotime("2014-12-06 07:45");
Output:
1417848300

Convert yyyy/mm/dd-hh-mm-ss.00 to Y-m-d H:i:s in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I'm developing a system that recibe a date format like: yyyy/mm/dd-hh-mm-ss.00 and I need convert it to Y-m-d H:i:s.
What I done was:
$newDate = date("Y-m-d H:i:s", strtotime($values[2]));
where $values[2] it is the date in that format, for instace: 2014/01/01-13-18-23.00. But the result is: 1970-01-01 01:00:00
Can you help me?
Thanks
You can try with strptime like
$format = "%Y-%m-%d %H:%i:%s";
echo strptime($values[2], $format);
Try using DateTime
$date = DateTime::createFromFormat('Y/m/d-H-i-s.00', '2014/01/01-13-18-23.00');
echo $date->format('Y-m-d H:i:s');
See demo here
You should be able to do something like this:
$date = DateTime::createFromFormat("Y/M/d-H-i-s.00", $values[2]);
echo $date->format("Y-m-d H:i:s");
The dateTime format for strtotime() function are predefined and you have to follow the rules to get desired result.
Mixed date time formats are available here PHP: Compound Formats - Manual

Categories