Can't get specific date from db -php - php

I'm trying to a date timestampted in my mysql db, but my code shows the current date instead.
This is the code that I'm trying:
$db_date = $row_news['nm_date'];
$year = substr($db_date, 0, 4);
$mon = substr($db_date, 4, 2);
$day = substr($db_date, 6, 2);
$orgdate = date("l dS F Y",mktime($mon, $day, $year));
$date = $orgdate
The script is meant to email $date, which should be the value of $row_news['nm_date'], but instead I get the current date "Saturday 11th September 2010.
Thanks for your help.

You're getting today's date because mktime() is not returning a valid value. If you check the manual entry for mktime() - http://www.php.net/mktime - you'll see that the parameter order is:
mktime ($hour, $minute, $second, $month, $day, $year)
so you probably want:
$orgdate = date("l dS F Y", mktime(12, 0, 0, $mon, $day, $year));
your code assumes that the date is in YYYYMMDD format (or YYYYMMDDHHIISS). Assuming that is correct, and it's not actually in date format (YYYY-MM-DD) then the above should fix your problem.
Edit: If the dates are in YYYY-MM-DD format you need to adjust your substrings to allow for the dashes:
$year = substr($db_date, 0, 4);
$mon = substr($db_date, 5, 2);
$day = substr($db_date, 8, 2);

$db_date = $row_news['nm_date'];
$orgdate = date("l dS F Y", $db_date);
$date = $orgdate;

Related

work with jmktime in JDF library

I have a problem with converting Jalali date to timestamp. For do that i'm using JDF library. So far I've Done this:
$jalalidate = $this->input->post('c_adddate');
list($year, $month, $day) = explode('/', $jalalidate);
$timestamp = jmktime(0, 0, 0, $month, $day, $year);
But it always show the current time!
echo date("Y-m-d", $timestamp);
Just Shows the current time.

convert a date in this format '031012' into a date that I can add days to

I have a date in this format 030512 (ddmmyy).
But I'm having trouble with converting this to a date usable format I can add days to.
Basically.. I extracted the date above from a text file, Now I need to be able to add a number of days to it. But I am having trouble parsing the date in this format.
Is there another way of doing this rather then something like this:
// I have a date in this format
$date = '030512'; // 03 May 2012
$day = substr($date,0,2);
$month = substr($date, 2,2);
$year = substr($date, 4,2);
$date_after = $day . "-" . $month . "-".$year;
// Now i need to add x days to this
$total_no_nights = 010; // must be this format
$days_to_add = ltrim($total_no_nights,"0"); // 10, remove leading zero
// how do i add 10 days to this date.
You can do this (php >= 5.3):
$date = DateTime::createFromFormat('dmy', '030512');
$date->modify('+1 day');
echo $date->format('Y-m-d');
http://www.php.net/manual/en/datetime.createfromformat.php
For php < 5.3 :
$dateArray = str_split('030512', 2);
$dateArray[2] += 2000;
echo date("d/m/Y", strtotime('+1 day', strtotime(implode('-', array_reverse($dateArray)))));
try this using the month/day/year you already have:
$date = "$month/$day/$year";
$change = '+10 day';
echo date("d/m/Y", strtotime($change, strtotime($date)));
Assuming the date will always be in the future (or at least after 1st Jan 2000), you're not far wrong:
// I have a date in this format
$date = '030512'; // 03 May 2012
$day = substr($date,0,2);
$month = substr($date, 2,2);
$year = substr($date, 4,2);
// dd-mm-yy is not a universal format but we can use mktime which also gives us a timestamp to use for manipulation
$date_after = mktime( 0, 0, 0, $month, $day, $year );
// Now i need to add x days to this
$total_no_nights = "010"; // must be this format
$days_to_add = intval( $total_no_nights ); // No need to use ltrim
// Here's the "magic". Again it returns a timestamp
$new_date = strtotime( "+$days_to_add days", $date_after );
Using the DateTime object would be easier but you say you're not on PHP5.3.
You can't do date manipulation with strings becase, well, they are not dates. In PHP, you can use Unix timestamps (which are actually integers...) or DateTime objects. In you case:
$timestamp = strtotime('-10 days', mktime(0, 0, 0, $month, $day, $year));
echo date('r', $timestamp);
... or:
$object = new DateTime("$year-$month-$day");
$object->modify('-10 days');
echo $object->format('r');
Also, please note that 010 is an octal number that corresponds to 8 in decimal.
using the convert function in sql the date can be obtained in appropriate format.
anter that operations can be performed in php to manipulate the date. i hope that answers your query.

convert string to date type in php

I have 3 variable $day , $month and $year and I want to create a date variable with these 3 variables in this fromat : yyyy-mm-dd in php.
$year variable is a persian year for example $year= 1390
this code works properly but not for persian date :
date("d-m-Y", mktime(0, 0, 0, $fMonth, $fDay, $fYear));
How I can do it ?
http://ir.php.net/manual/en/function.mktime.php
Usage:
$myTime = date("d/m/Y", mktime(0, 0, 0, $month, $day, $year));
So you'd want something like this:
date("d-M-Y", mktime(0, 0, 0, $month, $day, $year));
EDIT: Oops, sorry, fixed.
I'm not sure what you mean by date variable but. You can try these.
mktime()
mktime(0, 0, 0, $date, $month, $year)
or you can create a DateTime Object
$date = new DateTime('2000-01-01');
or you can try strtotime() if you have strings in your variables.

php - help converting timestamp string to readable time format

I have a record called Time with the following date string: 20100902040003 in the input file.
I need some php help to convert this to something more readable such as this format: 2010-09-02 04:00:03
and would like to format it as I print out the table data.
$timestamp = "20100902040003";
echo date('Y-m-d H:i:s', strtotime($timestamp)); // output: 2010-09-02 04:00:03
If you're sure that all records in your input file have the format YYYYMMDDHHmmss, you could try to split the string yourself and then using date() in conjunction with mktime() to generate a meaningful date format.
Since your data is already in the right format, it just needs some punctuation, so you can use a regular expression, rather than the date-parsing and date-formatting functions.
$timestamp = "20100902040003";
preg_replace('/(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/',
'\1-\2-\3 \4:\5:\6', $timestamp)
yields:
2010-09-02 04:00:03
Since the timestamp is not a unix timestamp, you have to use substr()
$timestamp = "20100902040003";
$year = substr($timestamp, 0, 3);
$month = substr($timestamp, 4, 5);
$day = substr($timestamp, 6, 7);
$hour = substr($timestamp, 8, 9);
$minute = substr($timestamp, 10, 11);
$second = substr($timestamp, 12, 13);
Then you arrange that using sprintf()
$formatted_timestamp = sprintf('%s-%s-%s %s:%s:%s', $year, $month, $day, $hour, $minute, $second);

First date of a week

I want a First Day Of a week say I have 45th week. Now I want to have date of the first sunday of this week.
Can any one suggest me how I can go about this ?
Thank you
Searching around a bit, the most suggested is:
$year = "2009"; // date("Y");
$week = "45"; // date("W");
$firstDayOfWeek = strtotime($year."W".str_pad($week,2,"0",STR_PAD_LEFT));
print("The first day of week ".$week." of ".$year." is ".date("D, d-m-Y",$firstDayOfWeek));
Basically this comes down to letting strtotime do the work for you. You fill in "2009W45" and it'll retrieve the date for you.
Pitfall here is that the week needs to be in a 2 digit format. So week 1 needs to be 01, hence the str_pad to zero-fill it.
you should strtotime and date function
some code like that
$next_sunday = strtotime('next sunday');
$next_week = strtotime('+1 week');
$next_sunday_of_next_week = strtotime('next sunday', $next_week);
hope this helps
If you have the month, day and year:
$timestamp = mktime(0, 0, 0, $month, $day, $year);
echo date('c', $timestamp) = mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year);
you could also do it this way if you have the timestamp:
echo $date('c', mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year));
From: http://pinoytech.org/blog/post/get-the-first-day-of-the-week-with-an-exact-date
date($str_format, strtotime($year."W".$week."1"))
Where $str_format is the output formate according to the PHP date() function.. I use 'M d Y'

Categories