extract date from datatime in php or Cakephp [duplicate] - php

This question already has answers here:
How to get time and date from datetime stamp in PHP?
(7 answers)
Closed 9 years ago.
i have a table in my cakephp which have a field name datetime and datatype is datetime
storing in this format
2013-06-18 00:00:00
I need to extract the date part of the value not the time ..
i extract time like this
$dateTime = $recentCall['Calllog']['dateTime'];
$time = date("H:i:s",strtotime($datetime));
now i want to extract the date part.which i dont know how can i do this .. i have done some research but nothing works out for me

This isn't exactly a CakePHP answer, but a PHP one.
$dateTime = $recentCall['Calllog']['dateTime'];
$timestamp = strtotime($dateTime);
$date = date('Y-m-d' , $timestamp);
$time = date('H:i:s' , $timestamp);
If you want to do somethink more Cakeish and use the CakePHP wrapper, should be:
$dateTime = $recentCall['Calllog']['dateTime'];
$timestamp = CakeTime::fromString($dateTime);
$date = CakeTime::format($dateTime, 'Y-m-d');
$time = CakeTime::format($dateTime, 'H:i:s');
I hope my answer is clear.
Ref: http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html

Use Y-m-d as first parameter in date to extract date.
$date = date("Y-m-d",strtotime($datetime));

Related

php convert date format Manual dd/mm/yyyy [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
Use date()
$sqldate = "2016-07-18";
$newDate = date("d-m-Y",$sqldate);
I also use
implode('-', array_reverse(explode('/', $sqldate)));
I am trying to convert a date from yyyy-mm-dd to dd-mm-yyyy (but not in SQL);
however I don't know how the date function requires a timestamp,
and I can't get a timestamp from this string.
I am getting particular date format. WHY? I tried get a date expected format, i tried php date but no success..
I excpected output
18/07/2016
$sqldate = "2016-03-21";
echo date('d/m/Y', strtotime($sqldate));
Use strtotime().
$sqldate = "2016-03-21";
echo $newDate = date("d/m/Y", strtotime($sqldate));
Output
21/03/2016
Live Demo : Click Here
$sqldate = "2016-03-21";
$newDate = date("d-m-Y",strtotime($sqldate));
this is what you need.

Convert database timestamp to date [duplicate]

This question already has answers here:
Formatting an SQL timestamp with PHP
(6 answers)
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a database timestamp with this format 2016-06-01 11:46:00 and I would like to convert and echo it to this format Wed. 01-06-2016.I tried a lot but no desirable results..Any ideas?
Thanks a lot, it worked!
If I want the difference between today and the past date in days or weeks I think I use date_diff.How exactly?
Simply use date and strtotime:
$time = '2016-06-01 11:46:00';
echo date("D. d-m-Y", strtotime($time)); //Wed. 01-06-2016
Updates:
$grk = array("Tet"); // complete the rest of the array
$eng = array("Wed"); // complete the rest of the array
$time = '2016-06-01 11:46:00';
$date = date("D. d-m-Y", strtotime($time));
echo str_replace($eng, $grk, $date);
$time = '2016-06-01 11:46:00';
echo date("D. d-m-Y", strtotime($time)); //Wed. 01-06-2016

Convert date and time format in passed date value from db [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a date value from the database and its format is: YYYY-mm-dd hh:mm:ss
I want to convert it to this format: YYYY/mm/dd hh:mm
Here is my code:
<?php
$datetime = $value['last_online']; //from db
$date = date('d-m-Y', $datetime);
$time = date('Gi.s', $datetime);
echo $date.' '.$time;
?>
But it's not working and is giving me an error of:
A non well formed numeric value encountered
I want to get rid of the minus signs and the seconds so my new date and time format would be YYYY/mm/dd hh:mm. How do I do it?
Any ideas?
Use like below
$datetime = $value['last_online']; //from db
$date = date('Y/m/d h:i', strtotime($datetime));

converting datetime in mysql with php [duplicate]

This question already has answers here:
Converting a datetime field timestamp from mysql to php
(2 answers)
Closed 9 years ago.
I have the following DateTime called 'StartTime' field in mysql with '1899-12-30 12:17:52'
I am trying to convert it to a time on the screen for the users with php using
date('h:i:s A',strtotime($row_get_student_times['StartTime']))
but for whatever reason it always returns 4:00 PM no matter what the time. Any clue why this is happening?
It's because you are trying to show a date before the uinx epoch (1970).
Try this:
$date = new DateTime($row_get_student_times['StartTime']);
echo $date->format("h:i:s A");
You need to use DateTime:
$datetime = new DateTime($row_get_student_times['StartTime']);
echo $datetime->format('h:i:s A');
<?php
$dateTimeString = '1899-12-30 12:17:52';
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $dateTimeString);
echo $dateTime->format('H:i:s A');

php date to timestamp [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert date to timestamp in PHP?
I want to transfer some date to timestamp ,the date formart is %day %month %time. Like 25 nov 07:44, I tried some method below, but the two dates are different. need a help, thanks.
<?php
$date = '25 nov 07:44';
$time = explode(" ",$date);
$minute = explode(":",$time[2]);
$timestamp = mktime(''.$minute[0].' '.$minute[1].' 00 '.$time[1].' '.$time[0].' 2011')."<br />";// mktime(%hour, %minute, %second, %month, %day, %year);
echo $timestamp; //1322202671
echo date("Y-m-j H:i:s", $timestamp); //2011-11-25 07:31:11
?>
A few problems:
You are feeding mktime a string instead of a comma separated list
$time[1] is a string and mktime needs a number for the month
You might want to give it a try with strtotime or otherwise add a translation table to go from your month strings to a number.
In recent versions of php (5.3+), you can use DateTime::createFromFormat().
Try something like
$format = 'd M H:i';
$date = DateTime::createFromFormat($format, $timestamp);
echo $date->format('Y-m-j H:i:s');
Personally, I find this class based approach a little cleaner.
PHP Docs:
DateTime::createFromFormat
date() for info on format strings

Categories