I have datetime 30-12-1899 9:25:52 AM here needed only time that is 9:25:52 AM .this time i wanted to insert in mysql database and this field has data type time.
my code is :
<?php
$date = "30-12-1899 9:25:52 AM";
$date = strtotime($date);
echo date('H:i:s', $date);
?>
when execute it returns:
01:00:00
i am not getting where is the problem.Can any one help me on this.
thank you.
Do the OOP way
<?php
$date1="30-12-1899 9:25:52 AM";
$format = 'd-m-Y H:i:s A';
$date = DateTime::createFromFormat($format, $date1);
echo $date->format('H:i:s A') . "\n";
$s = '30-12-1899 9:25:52 AM';
$dt = new DateTime($s);
$date = $dt->format('d-m-Y');
$time = $dt->format('h:i:s A');
echo $time;
see details here http://docs.php.net/class.datetime
actually the code does exactly what you want
<?php
$date = "30-12-1899 9:25:52 AM";
$date = strtotime($date);
echo date('H:i:s', $date);
?>
demo
Can you try this,
$date = "1899-12-30 9:25:52 AM";
echo $start_time = date("h:i:s A", strtotime($date));
OR
$date = "30-12-1899 9:25:52 AM";
$dtime = new DateTime($date);
echo $dtime->format("h:i:s A");
You get 01:00:00 as result, because on your version of PHP, strtotime() function returns invalid integer. More likely value false is returned, and when you format false via date() function, you will get 1970-01-01 00:00:00. You get time 01:00:00 because you have timezone offset, you are probably in UTC+1 timezone.
Here you can see, that your code will not work correctly on PHP version bellow 5.2.6, even on x64 machines, and results will be unreliable.
Best option would be to upgrade your PHP version (or change webhosting). But if you do not have this options, you can use string functions to break and concatenate date & time parts, like this:
$date = "30-12-1899 9:25:52 AM";
sscanf($date, "%d-%d-%d %d:%d:%d %s", $d, $m, $Y, $g, $i, $s, $A);
$h = $A == 'AM' ? $g : $g + 12;
echo "$Y-$m-$d $h:$i:$s"; # 1899-12-30 9:25:52
echo "$h:$i:$s"; # 9:25:52
You can try with explode like
$date = "30-12-1899 9:25:52 AM";
$date_arr = explode(' ',$date);
echo $date_arr[1];
This can be alternative
<?php
$date = "30-12-1899 9:25:52 AM";
$date = explode(' ',$date);
echo date[1].' '.date[2];
?>
Related
I'm learning PHP and I've been trying to get a two digit representation of a year from the array returned by the date_parse_from_format() function:
date_default_timezone_set('UTC');
$now = date('d.m.Y G:i:s');
$dateArray = date_parse_from_format("j.n.y H:iP", $now);
echo $dateArray['year'];
When I run this code, it prints 2020 on the browser instead of 14 as I expect. Please what am I missing in this code and how do I get the two digit year representation?
Try this
date_default_timezone_set('UTC');
$now = date('d.m.y G:i:s');
$dateArray = date_parse_from_format("j.n.Y H:iP", $now);
echo $dateArray['year'];
Try This
date_default_timezone_set('UTC');
$now = date('d.m.Y G:i:s');
$n = date ('j.n.y H:iP', strtotime($now)); //Convert to format you want
$dateArray = date_parse_from_format("j.n.y H:iP", $n);
echo '<pre>';
print_r($dateArray);
echo '</pre>';`
edit:
<?php
date_default_timezone_set('UTC');
$now = date('d.m.y G:i:s');
$dateArray = date_parse_from_format("j.n.Y H:iP", $now);
echo $dateArray['year'];
?>
date_parse_from_format() is available only in PHP 5.3,
Check this question as well,
PHP date_parse_from_format( ) alternative in PHP 5.2
In PHP if I have a variable ($getTimeStamp) that follows the format 0000-00-00 00:00:00 (i.e. 2013-09-26 13:06:00).
What is the easiest way to get the date ($getDate), hour ($getHour) and minute ($getMinute) as separate variables?
The easiest way is to use PHP DateTime class
$getTimeStamp = '2013-09-26 13:06:00';
$date = new \DateTime($getTimeStamp);
$dateString = $date->format('Y-m-d');
$hourString = $date->format('H');
$minuteString = $date->format('i');
It's not timestamp ;) Check what time() function returns, that's how timestamp looks like.
You can use something like that:
$time = strtotime($getTimeStamp);
$getDate = date('Y-m-d', $time);
$getHour = date('H', $time);
$getMinute = date('i', $time);
You can simply achieve this using -
$time = strtotime($getTimeStamp);
$getDate = date('Y-m-d', $time);
$getHour = date('H', $time);
$getMinute = date('i', $time);
Have a look at time() and date().
Well, since all you want is really to parse some text this would be the shortest way:
list($date, $hour, $minute) = preg_split('/[ :]/', $getTimeStamp);
There is no real reason to involve the date/time classes if you are not manipulating dates or calculating timestamps.
Live example.
Just one answer using the DateTime class
$dt = DateTime::createFromFormat('Y-m-d H:i:s', $getTimeStamp);
$date = $dt->format('Y-m-d ');
$hour = $dt->format('H');
$minutes = $dt->format('i');
I have a PHP date in the form of 2013-01-22 and I want to get tomorrows date in the same format, so for example 2013-01-23.
How is this possible with PHP?
Use DateTime
$datetime = new DateTime('tomorrow');
echo $datetime->format('Y-m-d H:i:s');
Or:
$datetime = new DateTime('2013-01-22');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
Or:
$datetime = new DateTime('2013-01-22');
$datetime->add(new DateInterval("P1D"));
echo $datetime->format('Y-m-d H:i:s');
Or in PHP 5.4+:
echo (new DateTime('2013-01-22'))->add(new DateInterval("P1D"))
->format('Y-m-d H:i:s');
$tomorrow = date("Y-m-d", strtotime('tomorrow'));
or
$tomorrow = date("Y-m-d", strtotime("+1 day"));
Help Link: STRTOTIME()
Since you tagged this with strtotime, you can use it with the +1 day modifier like so:
$tomorrow_timestamp = strtotime('+1 day', strtotime('2013-01-22'));
That said, it's a much better solution to use DateTime.
<? php
//1 Day = 24*60*60 = 86400
echo date("d-m-Y", time()+86400);
?>
echo date ('Y-m-d',strtotime('+1 day', strtotime($your_date)));
Use DateTime:
To get tomorrow from now :
$d = new DateTime('+1day');
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;
Results : 28/06/2017 08.13.20
To get tomorrow from a date :
$d = new DateTime('2017/06/10 08.16.35 +1day')
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;
Results : 11/06/2017 08.16.35
Hope it helps!
/**
* get tomorrow's date in the format requested, default to Y-m-d for MySQL (e.g. 2013-01-04)
*
* #param string
*
* #return string
*/
public static function getTomorrowsDate($format = 'Y-m-d')
{
$date = new DateTime();
$date->add(DateInterval::createFromDateString('tomorrow'));
return $date->format($format);
}
By strange it can seem it works perfectly fine: date_create( '2016-02-01 + 1 day' );
echo date_create( $your_date . ' + 1 day' )->format( 'Y-m-d' );
Should do it
here's working function
function plus_one_day($date){
$date2 = formatDate4db($date);
$date1 = str_replace('-', '/', $date2);
$tomorrow = date('Y-m-d',strtotime($date1 . "+1 days"));
return $tomorrow; }
$date = '2013-01-22';
$time = strtotime($date) + 86400;
echo date('Y-m-d', $time);
Where 86400 is the # of seconds in a day.
I have the following value:
30/05/2010 # 09:15:15
I need to convert it to Y-m-d H:i:s.
I've tried:
$date = "30/05/2010 # 09:15:15";
$formatteddate = date("Y-m-d H:i:s", time($date));
echo $formatteddate;
I end up with a value dated 1970. I've also tried strtotime.
Can anyone point out what I'm missing?
The time() function does not have any parameters which is why it is going to give you an error.
I have tried to use strtotime() thinking that may work, but it is not. I will update my answer when I find something that works. However, first thing is that time() will not work.
Edit: As Phil just beat me to seconds before:
$date = str_replace("# ", "", "30/05/2010 # 09:15:15");
$date = str_replace("/", "-", $date);
$formatteddate = date("Y-m-d H:i:s", strtotime($date));
echo $formatteddate;
Example is here: http://codepad.org/heph1PG0
If you're using PHP 5.3, try DateTime::createFromFormat(), eg
$dt = DateTime::createFromFormat('d/m/Y # H:i:s', $date);
If not, strtotime() may work but you'll need to get rid of the # symbol and change the forward slashes to hyphens (if that's an EU / AU date), eg
$time = strtotime(str_replace(array('#', '/'), array('', '-'), $date));
Edit:
To display the dates in the format you want, use
echo $dt->format('Y-m-d H:i:s'); // for DateTime
echo date('Y-m-d H:i:s', $time); // for strtotime
You have a bit of an odd format there... try date_parse_from_format.
http://www.php.net/manual/en/function.date-parse-from-format.php
$date = "30/05/2010 # 09:15:15";
$d = date_parse_from_format('m/d/Y # h:i:s', $date);
$formatted_date = "{$d['year']}-{$d['month']}-{$d['day']} {$d['hour']}:{$d['minute']}:{$d['second']}";
You have a very odd date format, so strtotime will have trouble. Instead we will use strptime which accepts a custom format:
$date = "30/05/2010 # 09:15:15";
$format = "%d/%m/%Y # %T";
$ftime = strptime($date, $format);
$timestamp = mktime(
$ftime['tm_hour'],
$ftime['tm_min'],
$ftime['tm_sec'],
// Because this is 0-11
$ftime['tm_mon'] + 1,
$ftime['tm_mday'],
// Because this is years since 1900
$ftime['tm_year'] + 1900
);
$formatteddate = date("Y-m-d H:i:s", $timestamp);
echo $formatteddate;
Result:
2010-05-30 09:15:15
If you're using PHP 5.3.0 or greater, you can use date_parse_from_format() to parse your custom formatted date.
If you're stuck on an older version of PHP, you'll have to parse it yourself. I've verified that this works:
<?php
function reformatDate($date) {
$matches = array();
if (!preg_match('/^(\d\d)\/(\d\d)\/(\d{4})\s*#\s*(\d\d):(\d\d):(\d\d)$/', $date, $matches)) {
throw new InvalidArgumentException('Invalid date supplied: ' . $date);
}
$day = $matches[1];
$month = $matches[2];
$year = $matches[3];
$hour = $matches[4];
$minute = $matches[5];
$second = $matches[6];
if ($day < 1 || $day > 31 || $month < 1 || $month > 12 || $hour > 24 || $minute > 60 || $second > 60) {
throw new InvalidArgumentException('Invalid date supplied: ' . $date);
}
return "$year-$month-$day $hour:$minute:$second";
}
echo reformatDate("30/05/2010 # 09:15:15");
?>
for all of you guyz date function will not format dates like 01/01/2045, this is something php restriction for dates having large years i.e 2039.
Try Like this...
<?php
$date = date_create('01-01-2001');
echo date_format($date, 'Y-m-d H:i:s');
?>
Say I have a string coming in, "2007-02-28", what's the simplest code I could write to turn that into "2007-03-01"? Right now I'm just using strtotime(), then adding 24*60*60, then using date(), but just wondering if there is a cleaner, simpler, or more clever way of doing it.
A clean way is to use strtotime()
$date = strtotime("+1 day", strtotime("2007-02-28"));
echo date("Y-m-d", $date);
Will give you the 2007-03-01
It's cleaner and simpler to add 86400. :)
The high-tech way is to do:
$date = new DateTime($input_date);
$date->modify('+1 day');
echo $date->format('Y-m-d');
but that's really only remotely worthwhile if you're doing, say, a sequence of transformations on the date, rather than just finding tomorrow.
You can do the addition right inside strtotime, e.g.
$today="2007-02-28";
$nextday=strftime("%Y-%m-%d", strtotime("$today +1 day"));
Another way is to use function mktime(). It is very useful function...
$date = "2007-02-28";
list($y,$m,$d)=explode('-',$date);
$date2 = Date("Y-m-d", mktime(0,0,0,$m,$d+1,$y));
but I think strtotime() is better in that situation...
The simplest way...
echo date('Y-m-d',strtotime("+1 day")); //from today
OR from specified date...
echo date('Y-m-d',strtotime("+1 day", strtotime('2007-02-28')));
Hello you can try this below especially if you are french
$date = date('l j F Y');
#increment the date
$date2 = date('l j F Y', strtotime("+7 day"));
to translate in french you can use the setlocale() function or the function below :
function fr_date($date){
$date = explode(' ', $date);
$date = str_replace('Monday','Lundi',$date);
$date = str_replace('Tuesday','Mardi',$date);
$date = str_replace('Wednesday','Mercredi',$date);
$date = str_replace('Thursday','Jeudi',$date);
$date = str_replace('Friday','Vendredi',$date);
$date = str_replace('Saturday','Samedi',$date);
$date = str_replace('Sunday','Dimanche',$date);
$date = str_replace('January','Janvier',$date);
$date = str_replace('February','Février',$date);
$date = str_replace('March','Mars',$date);
$date = str_replace('April','Avril',$date);
$date = str_replace('May','Mai',$date);
$date = str_replace('June','Juin',$date);
$date = str_replace('July','Juillet',$date);
$date = str_replace('August','Août',$date);
$date = str_replace('September','Septembre',$date);
$date = str_replace('October','Octobre',$date);
$date = str_replace('November','Novembre',$date);
$date = str_replace('December','Décembre',$date);
$date = implode(' ',$date);
return $date;
}
$your_date = strtotime("1month", strtotime(date("Y-m-d")));
$new_date = date("Y-m-d", $your_date++);
use strtotime() With date Formate
echo date('Y-m-d', strtotime('2007-02-28' . ' +1 day'));
$early_start_date = date2sql($_POST['early_leave_date']);
$date = new DateTime($early_start_date);
$date->modify('+1 day');
$date_a = new DateTime($early_start_date . ' ' . $_POST['start_hr'] . ':' . $_POST['start_mm']);
$date_b = new DateTime($date->format('Y-m-d') . ' ' . $_POST['end_hr'] . ':' . $_POST['end_mm']);
$interval = date_diff($date_a, $date_b);